[英]Return a value from vbscript function in c#
我正在用C#中的代碼執行.vbs文件,以檢查用戶的真實性。 我正在傳遞用戶名和密碼值,並在登錄時單擊.vbs將運行並驗證用戶。 如果用戶不可靠,則vbs中的函數將返回一個值,如何在c#中的代碼中獲取該值並使用它在應用程序的UI中顯示正確的錯誤消息。 請幫忙..
不是展示生產代碼,而是展示
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來簡化事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.