写输出到控制台从C#WinForms应用程序 [英] Writing output to the console from a c# winforms application

查看:346
本文介绍了写输出到控制台从C#WinForms应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:结果
如何显示在一个窗体应用程序控制台输出/窗?






有没有C#的WinForms程序写入到控制台窗口?


解决方案<办法/ DIV>

有基本上是可以在这里发生的两件事情。




  1. 控制台输出



这是可能的一个WinForms程序自身附加到(如果需要的或不同的控制台窗口,或实际上对新的控制台窗口)创建它的控制台窗口。一旦连接到控制台窗口Console.WriteLine()等按预期工作。有一个问题,以这种方式,该计划立即返回控制到控制台窗口,然后对其进行写入,所以用户还可以在控制台窗口中键入了。您可以使用启动与/等待参数来处理这个问题,我认为。



链接到start命令语法




  1. 重定向控制台输出



这是当有人管从程序别的地方,例如输出。



yourapp> file.txt的



附加到一个控制台窗口在这种情况下,有效地忽略管道。为了使这项工作,你可以调用Console.OpenStandardOutput()来得到一个处理流输出应通过管道输送到。仅当它的管道输出的作品,所以,如果你想处理这两种方案的你需要打开标准输出和写入,并连接到控制台窗口。这并不意味着输出发送到控制台窗口以管道,但其最好的解决办法我能找到。下面我用它来做到这一点的代码。

  //这始终写入父控制台窗口,也是一个标准输出重定向是否有一个。 
//这将是更好地做了相关的事(如写入重定向的文件(如果有),否则
//写入控制台),但它似乎并不可能。
公共类GUIConsoleWriter:IConsoleWriter
{
[System.Runtime.InteropServices.DllImport(KERNEL32.DLL)]
私人静态外部布尔AttachConsole(INT dwProcessId);

私人const int的ATTACH_PARENT_PROCESS = -1;

的StreamWriter _stdOutWriter;

//这必须在程序
公共GUIConsoleWriter()
{
叫早//这需要attachconsole之前发生。
//如果输出没有重定向,我们仍然可以得到一个有效的流,但它不会出现在任何地方写
//我猜它可能不会写的地方,但我无处可了解
VAR标准输出= Console.OpenStandardOutput();
_stdOutWriter =新的StreamWriter(标准输出);
_stdOutWriter.AutoFlush = TRUE;

AttachConsole(ATTACH_PARENT_PROCESS);
}

公共无效的WriteLine(串线)
{
_stdOutWriter.WriteLine(线);
Console.WriteLine(线);
}
}


Possible Duplicate:
How do I show console output/window in a forms application?

Is there a way for a c# winforms program to write to the console window?

解决方案

There are basically two things that can happen here.

  1. Console output

It is possible for a winforms program to attach itself to the console window that created it (or to a different console window, or indeed to a new console window if desired). Once attached to the console window Console.WriteLine() etc works as expected. One gotcha to this approach is that the program returns control to the console window immediately, and then carries on writing to it, so the user can also type away in the console window. You can use start with the /wait parameter to handle this I think.

Link to start Command syntax

  1. Redirected console output

This is when someone pipes the output from your program somewhere else, eg.

yourapp > file.txt

Attaching to a console window in this case effectively ignores the piping. To make this work you can call Console.OpenStandardOutput() to get a handle to the stream that the output should be piped to. This only works if the output is piped, so if you want to handle both of the scenarios you need to open the standard output and write to it and attach to the console window. This does mean that the output is sent to the console window and to the pipe but its the best solution I could find. Below the code I use to do this.

// This always writes to the parent console window and also to a redirected stdout if there is one.
// It would be better to do the relevant thing (eg write to the redirected file if there is one, otherwise
// write to the console) but it doesn't seem possible.
public class GUIConsoleWriter : IConsoleWriter
{
    [System.Runtime.InteropServices.DllImport("kernel32.dll")]
    private static extern bool AttachConsole(int dwProcessId);

    private const int ATTACH_PARENT_PROCESS = -1;

    StreamWriter _stdOutWriter;

    // this must be called early in the program
    public GUIConsoleWriter()
    {
        // this needs to happen before attachconsole.
        // If the output is not redirected we still get a valid stream but it doesn't appear to write anywhere
        // I guess it probably does write somewhere, but nowhere I can find out about
        var stdout = Console.OpenStandardOutput();
        _stdOutWriter = new StreamWriter(stdout);
        _stdOutWriter.AutoFlush = true;

        AttachConsole(ATTACH_PARENT_PROCESS);
    }

    public void WriteLine(string line)
    {
        _stdOutWriter.WriteLine(line);
        Console.WriteLine(line);
    }
}

这篇关于写输出到控制台从C#WinForms应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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