【发布时间】:2012-04-25 05:25:55
【问题描述】:
我认为之前关于这个主题的任何问题都没有给出这个问题的答案。我使用 psexec 执行远程 exe 文件。当我在命令行中运行它时,我得到了 exe 文件的输出。 psexec.exe \\machine C:\somename.exe.
当我使用 C sharp Process Execution 时,它要么挂起,要么不重定向输出。对于某些 exe,它超时,对于某些重定向标准输出为空,错误包含 Exe 退出,代码为 0。有什么方法可以捕获输出?
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName =GetPsExecPath();
startInfo.Arguments = arguments;
Debug.WriteLine(arguments);
startInfo.UseShellExecute = false;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = startInfo;
process.Start();
process.WaitForExit();
error = process.StandardError.ReadToEnd();
output = process.StandardOutput.ReadToEnd();
Debug.WriteLine(error);
Debug.WriteLine(output);
process.close();
编辑:索恩 所以这个问题主要是因为 Psexec 把很多其他的东西扔到了标准错误中,因此我们读取它们的顺序,如果我们使用 ReadToEnd() 可能会导致死锁。所以如果我们使用 BeginOutputReadLine 它就像一个魅力!
【问题讨论】:
-
向我们展示你用来执行 psexec 的代码。
-
如果我不重定向输出和错误,它会执行并以代码 0 退出。只有当我重定向它时才会导致这些 pblms。
-
当你通过命令行运行它时,它会返回数据吗?使用您在程序中传递的相同参数?
-
是的。exe实际上打印了一行,并且确实在命令行中打印了。
-
好的,您能否编辑您的问题...谢谢,现在我们知道某些 EXE 的超时和/或不返回 StdError 消息。也许您需要使用带有进程监视器的 Netmon/WireShark 来比较超时与无输出并首先修复超时。