2013-01-15 23 views
0

我正在执行c#中的代码来检查用户的真实性.vbs文件。我传递用户名和密码值,并在登录时单击.vbs将运行并验证用户。如果用户不是真实的,那么vbs中的函数会返回一个值,那么我如何在c#中的代码中获取该值,并使用它在应用程序的UI中显示正确的错误消息。 请帮助..从c#中的vbscript函数返回一个值#

+0

你如何运行VBS?你能解释一些代码来澄清? –

+0

Process.Start(“Execute.vbs”);我正在执行vbs文件。我现在需要的是将uname和pwd的值从winform UI传递到Execute中,并且该函数将返回一个字符串值,我必须再次在UI中显示它。 – user1909204

回答

1

并不是提供产品代码,而是要表明

  1. 那/它是如何工作的原则“
  2. 你有哪些关键字/组件查找在文档

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。