我正在使用c#中的代码执行.vbs文件,以检查用户的真实性。我正在传递用户名和密码值,并在登录时点击.vbs将运行并验证用户身份。如果用户不是真实的那么vbs中的函数返回一个值,我如何在c#中的代码中获取该值并使用它在应用程序的UI中显示正确的错误消息。 请帮忙..
答案 0 :(得分:1)
不提供生产代码,而是显示
demo.cs:
using System;
using System.Diagnostics;
namespace Demo
{
public class Demo
{
public static void Main(string [] args) {
string user = "nix";
if (1 <= args.Length) {user = args[0];};
string passw = "nix";
if (2 <= args.Length) {passw = args[1];};
string cscript = "cscript";
string cmd = string.Format("\"..\\vbs\\auth.vbs\" {0} {1}", user, passw);
System.Console.WriteLine("{0} {1}", cscript, cmd);
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.FileName = "cscript.exe";
process.StartInfo.Arguments = cmd;
try {
process.Start();
System.Console.WriteLine(process.StandardOutput.ReadToEnd());
System.Console.WriteLine(process.ExitCode);
} catch (Exception ex) {
System.Console.WriteLine(ex.ToString());
}
}
}
}
auth.vbs:
Option Explicit
Dim nRet : nRet = 2
If WScript.Arguments.Count = 2 Then
If "user" = WScript.Arguments(0) And "passw" = WScript.Arguments(1) Then
WScript.Echo "ok"
nRet = 0
Else
WScript.Echo "fail"
nRet = 1
End If
Else
WScript.Echo "bad"
End If
WScript.Quit nRet
输出:
demo.exe
cscript "..\vbs\auth.vbs" nix nix
fail
1
demo.exe user passw
cscript "..\vbs\auth.vbs" user passw
ok
0
也许您可以通过忘记文本返回来简化操作并仅使用.Exitcode。