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..
-
How are you run VBS? Can you explain some code to clarify?Hamlet Hakobyan– Hamlet Hakobyan2013-01-15 09:19:18 +00:00Commented 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.user1909204– user19092042013-01-15 09:49:52 +00:00Commented Jan 15, 2013 at 9:49
Add a comment
|
1 Answer
Not to present production code, but to show
- that/how it 'works in principle'
- 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.