gpt4 book ai didi

c# - PsExec v 1.98 输出重定向问题

转载 作者:太空宇宙 更新时间:2023-11-03 22:00:34 26 4
gpt4 key购买 nike

我认为之前关于这个主题的任何问题都没有给出这个问题的答案。我使用 psexec 来执行远程 exe 文件。当我在命令行中运行它时,我得到了 exe 文件的输出。 psexec.exe\\machine C:\somename.exe.
当我使用 C sharp Process Execution 时,它要么挂起,要么不重定向输出。对于某些 exe 超时,对于某些重定向标准输出为空并且错误包含以代码 0 退出的 Exe。有什么方法可以捕获输出吗?

 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 将很多其他东西扔到 stderr 中,因此我们读取它们的顺序,如果我们使用 ReadToEnd() 可能会导致死锁。因此,如果我们使用 BeginOutputReadLine,它就像一个魅力!

最佳答案

此代码段导致死锁的几率非常高。因为你先读 StandardError,然后读 StandardOutput。这意味着 process.StandardOutput.ReadToEnd() 在进程退出之前不会被调用。这意味着当 psexec 填满足够多的字符时,它无法刷新其 stdout 输出缓冲区。这意味着它会阻塞,因此永远不会终止。死锁城。

如果你交换这两个调用,你会有更好的机会,大多数程序将大部分输出发送到标准输出。但是,如果 psexec 出于某种原因向 stderr 写入大量字符,死锁的可能性仍然非零。您可以通过使用 BeginOutputReadLine 和 BeginErrorReadLine 来完全消除它。

关于c# - PsExec v 1.98 输出重定向问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10309817/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com