从c#中的vbscript函数返回值 [英] Return a value from vbscript function in c#

查看:264
本文介绍了从c#中的vbscript函数返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从c#中的代码执行.vbs文件以检查用户的真实性。我传递用户名和密码值,在登录点击.vbs将运行和认证用户。如果用户在不认证然后函数在vbs返回一个值,我怎么能在我的代码在c#中获得该值,并使用它在应用程序的UI中显示正确的错误消息。
请帮助..

解决方案

不显示生产代码,但显示






  • demo.cs:

      
    using System.Diagnostics;

    命名空间演示
    {
    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 = 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:

     选项显式

    Dim nRet:nRet = 2
    如果WScript。 Arguments.Count = 2 then
    如果user= WScript.Arguments(0)和passw= WScript.Arguments(1)然后
    WScript.Echook
    nRet = 0
    否则
    WScript.Echo失败
    nRet = 1
    结束如果
    否则
    WScript.Echo坏
    结束如果
    WScript.Quit nRet

    输出:

      demo.exe 
    cscript..\vbs\auth.vbsnix nix
    fail

    1

    demo.exe用户passw
    cscript..\vbs\auth.vbs用户passw
    ok

    0

    也许你可以通过忘记文本返回并简单地使用.Exitcode。


    I am executing a .vbs file from my code in c# for checking the authenticity of user. I am passing username and password values and on login click .vbs will run and authenticate the user. If the user in not authentic then function in vbs returns a value, how can i get that value in my code in c# and use it to display proper error message in UI of application. Please help..

    解决方案

    Not to present production code, but to show

    1. that/how it 'works in principle'
    2. what keywords/components you have to look up in the docs

    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
    

    output:

    demo.exe
    cscript "..\vbs\auth.vbs" nix nix
    fail
    
    1
    
    demo.exe user passw
    cscript "..\vbs\auth.vbs" user passw
    ok
    
    0
    

    Perhaps you can simplify things by forgetting the text return and use the .Exitcode only.

    这篇关于从c#中的vbscript函数返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆