2014-11-20 136 views
0

我的應用程序打開一個網站,然後運行一個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" 

回答

0
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(); 
} 
+1

建議修改您的答案以表明您推薦我做出的更改,以便於閱讀和理解答案 – Aeseir 2014-11-20 23:33:14