ProcessInfo和RedirectStandardOutput

Bra*_*tti 28 .net c# startprocessinfo redirectstandardoutput

我有一个应用程序在命令窗口中调用另一个进程,该进程已更新输出到控制台窗口的统计信息.我认为这是一个相当简单的操作,但我似乎无法让它工作.我错过了什么吗?

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

理想情况下,我想要的是输出在我触及的过程中发生变化,或者数据进入读者,我从中得到了事件.

任何帮助都会很棒,我觉得这是一个新手问题,但似乎缺少一些东西.

pat*_*jbs 49

我以前经历过这个.有时,您调用输出到控制台的进程的方式与此类输出重定向不兼容.在这种情况下,我很幸运能够修改外部进程来解决这个问题.

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

编辑:

我去了一个代码块,我曾经这样做过.这是在WPF应用程序中,它将进程输出重定向到窗口.注意事件绑定.由于这是WPF,我必须调用我的调用来写出数据.由于您不担心阻塞,因此您应该只需将其替换为:

Console.WriteLine(e.Data);
Run Code Online (Sandbox Code Playgroud)

希望它有所帮助!

    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);
            });
       }
    } 
Run Code Online (Sandbox Code Playgroud)

  • @David Lively,您可以尝试未记录的 PowerShell 参数 -InputFormat none。 (2认同)

Mic*_*tta 22

我不知道你正在运行什么问题分成,但如果你正在寻找只要它产生的作用于输出,尝试挂钩到进程的OutputDataReceived事件.您可以指定处理程序以从进程异步接收输出.我成功地使用了这种方法.

ProcessStartInfo info = new ProcessStartInfo(...)
info.UseShellExecute = false;
info.RedirectStandardOutput = true;
info.RedirectStandardError = true;

Process p = Process.Start(info);
p.OutputDataReceived += p_OutputDataReceived;
p.ErrorDataReceived += p_ErrorDataReceived;

p.BeginOutputReadLine();
p.BeginErrorReadLine();
p.WaitForExit();
Run Code Online (Sandbox Code Playgroud)

..

void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
  Console.WriteLine("Received from standard out: " + e.Data);
}

void p_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
  Console.WriteLine("Received from standard error: " + e.Data);
}
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅关闭Process 的OutputDataReceived事件.


aba*_*hev 11

使用lambda表达式等:

var info = new ProcessStartInfo(path)
{
    RedirectStandardError = true,
    RedirectStandardOutput = true,
    UseShellExecute = false,
    Verb = "runas",
};

var process = new Process
{
    EnableRaisingEvents = true,
    StartInfo = info
};

Action<object, DataReceivedEventArgs> actionWrite = (sender, e) =>
{
    Console.WriteLine(e.Data);
};

process.ErrorDataReceived += (sender, e) => actionWrite(sender, e);
process.OutputDataReceived += (sender, e) => actionWrite(sender, e);

process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
Run Code Online (Sandbox Code Playgroud)