C# 动态获取命令行输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12678407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting command line output dynamically
提问by HadiRj
I'm running a program using command line in c# this program produce some logs while its running in need to display this logs whenever it get change. I wrote the following code but it shows all the logs once the process has been killed and during the running time my program is not responding. how can I fix it?
我正在使用 c# 中的命令行运行一个程序,该程序会生成一些日志,而它在运行时需要在发生更改时显示此日志。我编写了以下代码,但是一旦进程被终止并且在运行期间我的程序没有响应,它就会显示所有日志。我该如何解决?
regards
问候
ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", "/c " + "C:\server.py");
Process proc = new Process();
procStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
procStartInfo.UseShellExecute = false;
procStartInfo.RedirectStandardOutput = true;
//procStartInfo.CreateNoWindow = true;
proc.StartInfo = procStartInfo;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit(300);
LogstextBox.Text = output;
Edited:well, I tried to use OutputDataReceivedbut it doesn't show any result, here is the changed code:
编辑:好吧,我尝试使用OutputDataReceived但它没有显示任何结果,这是更改后的代码:
{
//processCaller.FileName = @"ping";
//processCaller.Arguments = "4.2.2.4 -t"; this is working
processCaller.FileName = @"cmd.exe";
processCaller.Arguments = "/c c:\server.py"; //this is not working
processCaller.StdErrReceived += new DataReceivedHandler(writeStreamInfo);
processCaller.StdOutReceived += new DataReceivedHandler(writeStreamInfo);
processCaller.Completed += new EventHandler(processCompletedOrCanceled);
processCaller.Cancelled += new EventHandler(processCompletedOrCanceled);
this.richTextBox1.Text = "Server Started.." + Environment.NewLine;
processCaller.Start();
}
private void writeStreamInfo(object sender, DataReceivedEventArgs e)
{
this.richTextBox1.AppendText(e.Text + Environment.NewLine);
}
采纳答案by Jon Skeet
This is the problem:
这就是问题:
string output = proc.StandardOutput.ReadToEnd();
You won't get to the "end" of standard output until the process has terminated.
在进程终止之前,您不会到达标准输出的“结束”。
You should be reading a line at a time - or possibly just subscribing to the OutputDataReceivedevent (and following the documented other requirements for that event).
您应该一次阅读一行 - 或者可能只是订阅该OutputDataReceived事件(并遵循该事件记录的其他要求)。
EDIT: Here's sample code which works for me:
编辑:这是对我有用的示例代码:
using System;
using System.Diagnostics;
using System.Threading;
class Program
{
public static void Main()
{
ProcessStartInfo startInfo = new ProcessStartInfo("cmd", "/c " + "type Test.cs")
{
WindowStyle = ProcessWindowStyle.Hidden,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
Process process = Process.Start(startInfo);
process.OutputDataReceived += (sender, e) => Console.WriteLine(e.Data);
process.BeginOutputReadLine();
process.WaitForExit();
// We may not have received all the events yet!
Thread.Sleep(5000);
}
}
Note that in your sample code, you're accessing the UI on whatever thread the OutputDataReceivedhandler is called - that looks like a bad idea to me.
请注意,在您的示例代码中,您正在OutputDataReceived调用处理程序的任何线程上访问 UI - 这对我来说似乎是一个坏主意。
回答by Chimera
You can use the Process.BeginOutputReadLine Method. The link shows a complete working example in C# which uses the OutputDataReceived event. That code example should do what you want.
您可以使用Process.BeginOutputReadLine 方法。该链接显示了一个完整的 C# 工作示例,它使用OutputDataReceived event. 该代码示例应该可以满足您的需求。

