[英]Problem invoking external .exe using c#
我正在嘗試從我的C#代碼運行此.exe文件,它確實調用了.exe文件,但隨后中途崩潰了。 如果我在資源管理器上單擊.exe,它將完成其工作,所以我想知道我用來調用它的代碼是否存在問題:
string fileName = "loadscript.exe";
Utils.Logger.Info("Calling script:" + fileName);
Process process = new Process();
process.StartInfo.UseShellExecute = false;
process.StartInfo.FileName = fileName;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
Thread.Sleep(10000);
process.WaitForExit();
int exitCode = process.ExitCode;
string output = process.StandardOutput.ReadToEnd();
Utils.Logger.Info(".exe Output: ");
Utils.Logger.Info(output);
Thread.Sleep(10000);
process.WaitForExit();
int exitCode = process.ExitCode;
string output = process.StandardOutput.ReadToEnd();
在我看來,這似乎造成了僵局,這可能是最終崩潰的問題。 消除睡眠,然后嘗試以下操作:
string output = process.StandardOutput.ReadToEnd();
process.WaitForExit();
int exitCode = process.ExitCode;
請查看此問題的答案以獲取解釋:
process.StartInfo.UseShellExecute = false;
這要求您指定.exe文件的名稱。 將其設置為true時,將使用另一個Windows函數來啟動該文件,這一點足夠聰明,可以弄清楚.bat文件需要啟動cmd.exe來解釋.bat文件中的命令。
這就是您現在需要做的事情,FileName必須為“ cmd.exe”,Arguments屬性必須為“ loadscript.bat”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.