我需要在我的c#程序中執行此操作。運行命令並等待在c#中完成#
Process process = LaunchCommandWindow("Special args and environment set")
process.StandardInput.WriteLine("cmd1");
process.StandardInput.WriteLine("cmd2");
//Parse logs
using (Streamreader sr = new())
{ etc etc..}
我的命令執行正確,但問題是,解析記錄代碼犯規等待CMD1和CMD2執行完成。我無法致電退出和處理。 waitforexit因爲在解析日誌之後,我有更多的命令可以執行。
以下是如何設置流程啓動信息。 LaunchCommandWindow()
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = Path.Combine(Environment.SystemDirectory, "cmd.exe");
startInfo.Arguments = "/K " + newEnvironmentArgs;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardInput = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
return process;
我如何解析日誌代碼等待CMD2完成?
謝謝。
這將工作,雖然主線程將進入忙碌的等待狀態,同時它會查找完成消息。一個新的線程會阻止程序進入「無響應」狀態。 – Jeff 2011-02-01 19:10:40
+1爲了做到這一點,他還需要將`RedirectStandardOutput`設置爲true。 – 2011-02-01 19:10:54