控制台输出重定向到文本框在单独的程序控制台、文本框、重定向、程序

2023-09-02 11:29:42 作者:メ霸气ヴ爷ャ

我开发一个Windows窗体应用程序需要我打电话给一个单独的程序来执行任务。该方案是一个控制台应用程序,我需要从控制台的标准输出重定向到在我的程序一个文本框。

我没有问题,从我的应用程序执行的程序,但我不知道如何将输出重定向到我的申请。我需要而在程序运行使用事件来捕获输出。

控制台程序并不意味着停止运行,直到我的应用程序停止和文字在不断的变化,在随机时间间隔。我正在试图做的是简单地挂钩输出从控制台来触发事件处理程序,然后可以用来更新文本框。

我正在使用C#code中的程序,并使用.NET框架开发。原来的应用程序不是.NET程序。

编辑: 这里的例子$ C $的什么,我试图做℃。在我最后的应用程序,我将与code替换Console.WriteLine更新文本框。我想在我的事件处理程序设置断点,它甚至没有达到。

  void方法()
    {
        VAR P =新的Process();
        VAR路径= @C: ConsoleApp.exe;

        p.StartInfo.FileName =路径;
        p.StartInfo.UseShellExecute = FALSE;
        p.OutputDataReceived + = p_OutputDataReceived;

        p.Start();
    }

    静态无效p_OutputDataReceived(对象发件人,DataReceivedEventArgs E)
    {
        Console.WriteLine(>>> {0},e.Data);
    }
 

解决方案

这对我的作品:

 无效RunWithRedirect(字符串cmdPath)
{
    VAR PROC =新工艺();
    proc.StartInfo.FileName = cmdPath;

    //设置输出重定向
    proc.StartInfo.RedirectStandardOutput =真;
    proc.StartInfo.RedirectStandardError = TRUE;
    proc.EnableRaisingEvents = TRUE;
    proc.StartInfo.CreateNoWindow = TRUE;
    //参见下面的输出处理程序
    proc.ErrorDataReceived + = proc_DataReceived;
    proc.OutputDataReceived + = proc_DataReceived;

    proc.Start();

    proc.BeginErrorReadLine();
    proc.BeginOutputReadLine();

    proc.WaitForExit();
}

无效proc_DataReceived(对象发件人,DataReceivedEventArgs E)
{
    //输出将字符串e.Data
}
 
第八章 字符输入输出和输入验证

I'm developing an Windows Forms application that requires me to call a separate program to perform a task. The program is a console application and I need to redirect standard output from the console to a TextBox in my program.

I have no problem executing the program from my application, but I don't know how to redirect the output to my application. I need to capture output while the program is running using events.

The console program isn't meant to stop running until my application stops and the text changes constantly at random intervals. What I'm attempting to do is simply hook output from the console to trigger an event handler which can then be used to update the TextBox.

I am using C# to code the program and using the .NET framework for development. The original application is not a .NET program.

EDIT: Here's example code of what I'm trying to do. In my final app, I'll replace Console.WriteLine with code to update the TextBox. I tried to set a breakpoint in my event handler, and it isn't even reached.

    void Method()
    {
        var p = new Process();
        var path = @"C:ConsoleApp.exe";

        p.StartInfo.FileName = path;
        p.StartInfo.UseShellExecute = false;
        p.OutputDataReceived += p_OutputDataReceived;

        p.Start();
    }

    static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
    {
        Console.WriteLine(">>> {0}", e.Data);
    }

解决方案

This works for me:

void RunWithRedirect(string cmdPath)
{
    var proc = new Process();
    proc.StartInfo.FileName = cmdPath;

    // set up output redirection
    proc.StartInfo.RedirectStandardOutput = true;
    proc.StartInfo.RedirectStandardError = true;    
    proc.EnableRaisingEvents = true;
    proc.StartInfo.CreateNoWindow = true;
    // see below for output handler
    proc.ErrorDataReceived += proc_DataReceived;
    proc.OutputDataReceived += proc_DataReceived;

    proc.Start();

    proc.BeginErrorReadLine();
    proc.BeginOutputReadLine();

    proc.WaitForExit();
}

void proc_DataReceived(object sender, DataReceivedEventArgs e)
{
    // output will be in string e.Data
}