Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重定向进程输出C#_C#_Process_Redirectstandardoutput_Output Redirect - Fatal编程技术网

重定向进程输出C#

重定向进程输出C#,c#,process,redirectstandardoutput,output-redirect,C#,Process,Redirectstandardoutput,Output Redirect,我想将流程的标准输出重定向到字符串,以便稍后进行解析。 我还希望在进程运行时看到屏幕上的输出,而不仅仅是在进程运行结束时 这可能吗?使用 来自MSDN的样本: // Start the child process. Process p = new Process(); // Redirect the output stream of the child process. p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStanda

我想将流程的标准输出重定向到字符串,以便稍后进行解析。 我还希望在进程运行时看到屏幕上的输出,而不仅仅是在进程运行结束时

这可能吗?

使用

来自MSDN的样本:

// Start the child process.
Process p = new Process();
// Redirect the output stream of the child process.
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "Write500Lines.exe";
p.Start();
// Do not wait for the child process to exit before
// reading to the end of its redirected stream.
// p.WaitForExit();
// Read the output stream first and then wait.
string output = p.StandardOutput.ReadToEnd();
p.WaitForExit();

另请参阅和,以了解
ReadToEnd()
的替代方案,它将更好地满足您的“在流程运行时查看输出”要求

如果要从c#应用程序执行exe并从中获取输出,则可以使用以下代码

System.Diagnostics.Process p = new System.Diagnostics.Process();            

p.StartInfo.FileName = "PATH TO YOUR FILE";
p.StartInfo.UseShellExecute = false;
p.StartInfo.Arguments = metalType + " " + graphHeight + " " + graphWidth;
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;              

p.EnableRaisingEvents = true;
p.Start();            
svgText = p.StandardOutput.ReadToEnd();

using(StreamReader s = p.StandardError)
{
    string error = s.ReadToEnd();
    p.WaitForExit(20000);
}

不要忘记写p.EnableRaisingEvents=true

请提供更多详细信息,说明您试图复制的内容。我认为以下内容也很相关。--但请稍候,还有更多:--
EnableRaisingEvents
仅用于
进程。已退出
事件。