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

时间:2009-01-06 06:20:23

标签: c# .net winforms textbox console

我正在开发一个Windows窗体应用程序,它要求我调用一个单独的程序来执行任务。该程序是一个控制台应用程序,我需要将标准输出从控制台重定向到我的程序中的TextBox。

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

控制台程序并不意味着在我的应用程序停止并且文本以随机间隔不断变化之前停止运行。我试图做的只是从控制台挂钩输出以触发事件处理程序,然后可以使用它来更新TextBox。

我使用C#编写程序代码并使用.NET框架进行开发。原始应用程序不是.NET程序。

编辑: 这是我正在尝试做的示例代码。在我的最终应用程序中,我将用代码替换Console.WriteLine来更新TextBox。我试图在我的事件处理程序中设置断点,甚至没有达到它。

    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);
    }

5 个答案:

答案 0 :(得分:70)

这对我有用:

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
}

答案 1 :(得分:4)

您可以使用以下代码

        MemoryStream mem = new MemoryStream(1000);
        StreamWriter writer = new StreamWriter(mem);
        Console.SetOut(writer);

        Assembly assembly = Assembly.LoadFrom(@"C:\ConsoleApp.exe");
        assembly.EntryPoint.Invoke(null, null);
        writer.Close();

        string s = Encoding.Default.GetString(mem.ToArray());
        mem.Close();

答案 2 :(得分:1)

答案 3 :(得分:1)

我在O2 Platform(开源项目)中添加了一些辅助方法,允许您通过控制台输出和输入轻松编写与其他进程的交互脚本(参见http://code.google.com/p/o2platform/source/browse/trunk/O2_Scripts/APIs/Windows/CmdExe/CmdExeAPI.cs

对您来说也很有用,可能是允许查看当前进程的控制台输出的API(在现有控件或弹出窗口中)。有关更多详细信息,请参阅此博客文章:http://o2platform.wordpress.com/2011/11/26/api_consoleout-cs-inprocess-capture-of-the-console-output/(此博客还包含有关如何使用新进程的控制台输出的详细信息)

答案 4 :(得分:0)

感谢Marc Maxham的回答让我省时间!

当所有交易的Jon注意到它时,UseShellExecute必须设置为false才能重定向IO流,否则Start()调用会抛出InvalidOperationException

以下是我对txtOut是WPF只读文本框

的代码的修改
void RunWithRedirect(string cmdargs)
{
    var proc = new Process()
    {
        StartInfo = new ProcessStartInfo("cmd.exe", "/k " + cmdargs)
        {
            RedirectStandardOutput = true,
            RedirectStandardError = true,
            CreateNoWindow = true
        },
        EnableRaisingEvents = 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)
{
    if (e.Data != null)
        Dispatcher.BeginInvoke(new Action( () => txtOut.Text += (Environment.NewLine + e.Data) ));
}
相关问题