49

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

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

控制台程序不会停止运行,直到我的应用程序停止并且文本以随机间隔不断变化。我正在尝试做的只是挂钩来自控制台的输出以触发事件处理程序,然后该处理程序可用于更新 TextBox。

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

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

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

4 回答 4

72

这对我有用:

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
}
于 2009-01-06T06:46:59.870 回答
5

您可以使用以下代码

        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();
于 2009-01-06T12:11:19.683 回答
1

我在O2 平台(开源项目)中添加了许多辅助方法,允许您通过控制台输出和输入轻松编写与另一个进程的交互脚本(请参阅http://code.google.com/p/o2platform/源/浏览/主干/O2_Scripts/APIs/Windows/CmdExe/CmdExeAPI.cs

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

于 2011-11-26T18:01:48.810 回答
0

感谢 Marc Maxham 的回答,为我节省了时间!

正如 Jon of All Trades 所注意到的,UseShellExecute必须设置为 false 才能重定向 IO 流,否则Start()调用会抛出InvalidOperationException.

这是我对txtOutWPF 只读文本框的代码的修改

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) ));
}
于 2015-02-17T14:41:06.380 回答