PsExec&进程通过C#不显示输出

显然PsExec正在工作。 此代码工作。 执行时,它永远不会返回错误。 但只有有时候它会返回输出,当它返回输出时,它总是第一行。 ipconfig的第一行是“Windows IP Configuration”,错误框显示“ipconfig返回错误代码0”。 如果用本地IPreplaceREMOTEPC,它将运行,您可以看到cmd短暂地闪烁着信息。 所以它的工作。

我应该补充说,我相信原因是执行PsExec后,它不会“退出”。 所以WaitForExit从来没有发生。 当您从cmd窗口中单击X时,框出现。 有时输出提到,或空白。

但它从来没有输出整个事情的一些爆炸的原因! 它从来没有读到最后。

private void button1_Click(object sender, EventArgs e) { Process p = new Process(); p.StartInfo.FileName = @"C:\PsExec.exe"; p.StartInfo.Arguments = @"\\REMOTEPC -u USER -p PASS -s ipconfig /all"; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; //p.StartInfo.RedirectStandardInput = true; p.Start(); string output = p.StandardOutput.ReadToEnd(); //Tried this method of getting the output too // while (!p.HasExited) //{ // output += p.StandardOutput.ReadToEnd(); // } string error = p.StandardError.ReadToEnd(); p.WaitForExit(); MessageBox.Show(output); MessageBox.Show(error); }