1

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..

2
  • How are you run VBS? Can you explain some code to clarify? Commented Jan 15, 2013 at 9:19
  • Process.Start("Execute.vbs"); I am executing the vbs file. What i need now is to pass values of uname and pwd from winform UI to function in Execute and that function will return a string value which i have to display it in UI again. Commented Jan 15, 2013 at 9:49

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.