process.standardoutput.ReadToEnd()总是空的?

ale*_*lex 13 c# process

我正在启动一个控制台应用程序,但是当我重定向标准输出时,我总是得不到任何东西!

当我不重定向它并设置CreateNoWindow为时false,我在控制台中看到了所有内容,但是当我重定向它时,StandardOutput.ReadToEnd()总是返回一个空字符串.

        Process cproc = new Process();
        cproc.StartInfo.CreateNoWindow = true;
        cproc.StartInfo.FileName = Dest;
        cproc.StartInfo.RedirectStandardOutput = true;
        cproc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        cproc.StartInfo.UseShellExecute = false;
        cproc.EnableRaisingEvents = true;
        cproc.Start();
        cproc.Exited += new EventHandler(cproc_Exited);
        while(!stop)
        {
           result += cproc.StandardOutput.ReadToEnd();
        }
Run Code Online (Sandbox Code Playgroud)

EventHandler cproc_exited刚刚设置stoptrue.有人能解释为什么result总是这样string.Empty吗?

小智 16

最好的方法是重定向输出并等待事件:

    // not sure if all this flags are needed
    process.StartInfo.CreateNoWindow = true;
    process.StartInfo.ErrorDialog = false;
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardError = true;
    process.StartInfo.RedirectStandardInput = true;
    process.StartInfo.RedirectStandardOutput = true;
    process.EnableRaisingEvents = true;
    process.OutputDataReceived += process_OutputDataReceived;
    process.ErrorDataReceived += process_ErrorDataReceived;
    process.Exited += process_Exited;
    process.Start();

    void process_Exited(object sender, System.EventArgs e)
    {
        // do something when process terminates;
    }

    void process_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        // a line is writen to the out stream. you can use it like:
        string s = e.Data;
    }

    void process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        // a line is writen to the out stream. you can use it like:
        string s = e.Data;
    }
Run Code Online (Sandbox Code Playgroud)


Jon*_*eet 8

你为什么要循环?一旦它被读到最后,它将无法再读取任何数据,是吗?

你确定文本实际上是写入StandardOutput而不是StandardError吗?

(是的,显然你想设置RedirectStandardOutput为true而不是false.我认为这只是你复制错误版本代码的情况.)

编辑:正如我在评论中所建议的那样,您应该从单独的线程中读取标准输出和标准错误.千万不能等到进程已退出-这可以用一个僵局,在你等待的进程退出结束了,但是这个过程是阻止试图写入到stderr /标准输出,因为你还没有从缓冲区中读取.

或者,您可以订阅OutputDataReceived和ErrorDataReceived事件,以避免使用额外的线程.


Dir*_*mar 7

您已禁用标准输出的重定向.尝试改变

cproc.StartInfo.RedirectStandardOutput = false;
Run Code Online (Sandbox Code Playgroud)

cproc.StartInfo.RedirectStandardOutput = true;
Run Code Online (Sandbox Code Playgroud)

以下来自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();
Run Code Online (Sandbox Code Playgroud)