ProcessInfo和RedirectStandardOutput [英] ProcessInfo and RedirectStandardOutput

查看:226
本文介绍了ProcessInfo和RedirectStandardOutput的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序调用另外一个进程在命令窗口和进程更新的统计数据输出到控制台窗口。我认为这是一个相当简单的操作,但我似乎无法得到它的工作。我失去了一些东西?

I have an app which calls another process in a command window and that process has updating stats that output to the console window. I thought this was a fairly simple operation but I can't seem to get it to work. Am I missing something?

string assemblyLocation = Assembly.GetExecutingAssembly().Location;

Process process = new Process
{
    ProcessStart =
    {
        RedirectStandardOutput = true,
        UseShellExecute = false,
        WindowStyle = ProcessWindowStyle.Hidden,
        Arguments = arg,
        FileName = assemblyLocation.Substring(0, assemblyLocation.LastIndexOf("\\")) + "\\ffmpeg.exe",
        CreateNoWindow = true
    }
};

process.Start();

Console.WriteLine(process.StandardOutput.ReadToEnd());

process.WaitForExit();

理想的情况是什么,我想是因为这个过程中的输出变化,我打或数据进入,我得到的事件关闭它的读者。

Ideally what I would like is as the output changes within that process I hit or data comes into the reader that I get events off it.

任何帮助将是巨大的,我觉得这是一个新手的问​​题,但似乎失去了一些东西。

Any help would be great, I feel like this is a newbie question but seem to be missing something.

推荐答案

我以前经历过这一点。有的时候,以何种方式你调用输出到控制台的过程中是不是有这种输出重定向兼容。我在这种情况下,一直有幸能够修改外部进程来解决这个问题。

I've experienced this before. Sometimes, the way in which the process you're calling outputs to the console is not compatible with this sort of output redirection. I've been fortunate enough in this case to be able to modify the external process to get around this.

您可以尝试在另一个进程输出到控制台上运行您的code,看看它是否工作正常。它读取有关权利,我现在。

You might try running your code on another process that outputs to the console, and see if it works properly. It reads about right to me right now.

编辑:

我去了,拉着我用做这一个code座。这是一个WPF应用程序,它把过程输出到窗口。注意事件绑定。由于这是WPF我必须调用我的电话写出来的数据。既然你不担心堵塞,OU应该能够简单地替换成:

I went and pulled a code block I've used to do this. This is in a WPF app which redirects the process output to the window. Notice the event binding. Since this is WPF I have to invoke my call to write the data out. Since you aren't worried about blocking, ou should be able to simply replace that with:

Console.WriteLine(e.Data);

希望这有助于!

Hopefully it helps!

    private static void LaunchProcess()
    {
        Process build = new Process();
        build.StartInfo.WorkingDirectory =  @"dir";
        build.StartInfo.Arguments = "";
        build.StartInfo.FileName = "my.exe";

        build.StartInfo.UseShellExecute = false;
        build.StartInfo.RedirectStandardOutput = true;
        build.StartInfo.RedirectStandardError = true;
        build.StartInfo.CreateNoWindow = true;
        build.ErrorDataReceived += build_ErrorDataReceived;
        build.OutputDataReceived += build_ErrorDataReceived;
        build.EnableRaisingEvents = true;
        build.Start();
        build.BeginOutputReadLine();
        build.BeginErrorReadLine();
        build.WaitForExit();
    }

    // write out info to the display window
    static void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)
    {
        string strMessage = e.Data;
        if (richTextBox != null && !String.Empty(strMessage))
        {
            App.Instance.Dispatcher.BeginInvoke(DispatcherPriority.Send, (ThreadStart)delegate()
            {
                Paragraph para = new Paragraph(new Run(strMessage));
                para.Margin = new Thickness(0);
                para.Background = brushErrorBrush;
                box.Document.Blocks.Add(para);
            });
       }
    } 

这篇关于ProcessInfo和RedirectStandardOutput的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆