如何使用C#从我的VBScript控制台获取输出?
问题描述:
我的应用程序打开一个网站,然后运行一个VBS文件来做一些数据输入。数据输入完成后,我想退出应用程序。如何使用C#从我的VBScript控制台获取输出?
在我当前的迭代中,VBS文件执行并且我的C#代码继续执行(在数据输入完成之前退出Web应用程序)。
Process.Start(appPath + @"external\website.url");
getAllProcesses(false);
ProcessStartInfo startInfo = new ProcessStartInfo(appPath + @"\external\UNLOCK.vbs", employeeID);
Process scriptProc = new Process();
scriptProc.StartInfo.FileName = @"cscript";
scriptProc.StartInfo.WorkingDirectory = appPath + @"external\";
scriptProc.StartInfo.Arguments = "UNLOCK.vbs " + employeeID;
scriptProc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //prevent console window from popping up
scriptProc.StartInfo.RedirectStandardError = true;
scriptProc.StartInfo.RedirectStandardInput = true;
scriptProc.StartInfo.RedirectStandardOutput = true;
scriptProc.StartInfo.ErrorDialog = false;
scriptProc.StartInfo.UseShellExecute = false;
scriptProc.Start();
scriptProc.WaitForExit(); // <-- Optional if you want program running until your script exit
Read(scriptProc.StandardOutput);
Read(scriptProc.StandardError);
while(true)
{
String completed = Console.ReadLine();
scriptProc.StandardInput.WriteLine(completed);
if(completed.CompareTo("Completed") == 0)
{
break;
}
}
if (scriptProc.HasExited)
{
getAllProcesses(true);
Application.Exit();
}
scriptProc.Close();
我想只执行
getAllProcesses(true);
Application.Exit();
后,才从我的VBS文件,上面写着“已完成”输出。
我的VBS文件中有一行在最后说
WScript.Echo "Completed"
。
答
Process scriptProc = new Process();
ProcessStartInfo info = new ProcessStartInfo();
info.WorkingDirectory = appPath + @"external\";
info.FileName = "Cscript.exe";
info.Arguments = "UNLOCK.vbs" + employeeID;
info.RedirectStandardError = true;
info.RedirectStandardInput = true;
info.RedirectStandardOutput = true;
info.UseShellExecute = false;
info.WindowStyle = ProcessWindowStyle.Hidden;
scriptProc.StartInfo = info;
scriptProc.Start();
scriptProc.WaitForExit();
bool exit = false;
while (!scriptProc.StandardOutput.EndOfStream)
{
if (scriptProc.StandardOutput.ReadLine() == "Completed")
{
exit = true;
break;
}
}
if (exit == true)
{
getAllProcesses(true);
Application.Exit();
}
建议修改您的答案以表明您推荐我做出的更改,以便于阅读和理解答案 – Aeseir 2014-11-20 23:33:14