我的問題是執行給定命令時沒有輸出。
該 exe 需要相當長的時間才能完成,而它正在執行時,我期望輸出連續的資料流。但由于某種原因,我的程式似乎只是忽略了來自 exe 的輸出。
我覺得我已經嘗試了一切,但沒有運氣。
我已經嘗試過其他命令,例如“tree c:/”,并且我能夠很好地接收到該輸出。我確定我正在執行的命令是有效的。我已經在單獨的 cmd 中單獨測驗了該命令,它可以正常作業。
我也嘗試過僅在 exe 上運行該行程。這也產生了輸出。
我已經堅持了一段時間了,所以任何幫助將不勝感激。
Task.Run(() =>
{
string command = downloadPath @"\myinstall.exe quickinstallall";
var startInfo = new ProcessStartInfo("cmd.exe")
{
WorkingDirectory = downloadPath,
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardError = true,
RedirectStandardOutput = true,
WindowStyle = ProcessWindowStyle.Normal,
CreateNoWindow = false,
Verb = "runas"
};
Process cmd = new Process { StartInfo = startInfo };
cmd.EnableRaisingEvents = true;
cmd.OutputDataReceived = UpdateOutput;
cmd.ErrorDataReceived = UpdateOutput;
cmd.Start();
cmd.BeginOutputReadLine();
cmd.StandardInput.WriteLine(command);
cmd.BeginErrorReadLine();
cmd.WaitForExit();
});
uj5u.com熱心網友回復:
好的,所以我弄清楚了為什么沒有記錄輸出。我只是錯過了兩行代碼......
cmd.StandardInput.Flush();
cmd.StandardInput.Close();
和
我不得不洗掉runas并從應用程式本身繼承管理員權限。通過“繼承管理員權限”,我的意思只是確保應用程式始終以管理員身份執行。
我不知道為什么這完全解決了我的問題,但它現在似乎作業得很好。如果有人知道為什么這有效,我很想知道。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/477905.html
上一篇:在C#中鍵入種姓Serilog
