从WPF Windows应用程序实际控制台输出Console.WriteLine [英] Output Console.WriteLine from WPF Windows Applications to actual console

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

问题描述

背景:我奋力添加命令行和批量处理功能,以现有的WPF的 Windows应用程序的。当我在启动时检测到一些选项我想喝preSS出现的窗口中,做一些处理和immedietally退出。现在,因为没有用户界面,我想输出一些消息到标准输出/标准错误。考虑以下code:

Background: I am struggling to add command line and batch processing capabilities to an existing WPF Windows Application. When I detect some options at startup I suppress the window from appearing, do some processing and quit immedietally. Now, because there's no UI I'd like to output some messages to stdout/stderr. Consider following code:

namespace WpfConsoleTest
{
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            Console.WriteLine("Start");
            System.Threading.Thread.Sleep(1000);
            Console.WriteLine("Stop");
            Shutdown(0);
        }
    }
}

当我运行命令行我希望下面的输出:

When I run from command line I would expect following output:

Start
Stop

但相反:

C:\test>WpfConsoleTest.exe

C:\test>

可以输出重定向,虽然:

C:\test>WpfConsoleTest.exe > out.txt

C:\test>type out.txt
Start
Stop

重定向到CON不工作,遗憾的是:

Redirecting to CON does not work, unfortunately:

C:\test>WpfConsoleTest.exe > CON

C:\test>

另一个问题是,WpfConsoleTest.exe退出 immedietaly 开始之后。所以:

C:\test>WpfConsoleTest.exe > out.txt & type out.txt

C:\test>

不过:<​​/ P>

But:

C:\test>WpfConsoleTest.exe > out.txt & ping localhost > nul & type out.txt
Start
Stop

最好的解决办法我能够来到迄今是使用启动/ B /等待

C:\test>start /B /wait WpfConsoleTest.exe > out.txt & type out.txt
Start
Stop

此方法主要是确定 - 如果你在​​一个蝙蝠包起来就可以preserve错误code等。一个巨大的缺陷是你得到的输出应用程序结束后,即有没有办法可以跟踪进度,你必须等待你的心里去到结束。

This approach is mostly ok - if you wrap it up in a bat you can preserve error code and so on. The one huge downfall is that you get output after application ends, i.e. there's no way you can track progress, you have to wait for whatever is going on to finish.

所以,我的问题是:??如何输出到父母的控制台WPF的 Windows应用程序同时,为什么这么难从WPF抢标准输出/标准错误

Therefore, my question is: How to output to parent console from WPF Windows Application? Also, why is so hard to grab stdout/stderr from WPF?

我知道我可以申请类型更改为的控制台应用程序的项目设置,但是这有一个讨厌的副作用 - 控制台窗口是可见的时候,即使你只需双击exe文件。 该解决方案也不会做的,因为它创建的控制台,即使是应用从CMD运行。

I know that I could change application type to Console Application in project settings, but this has a nasty side effect - console window is visible all the time, even if you simply double click the exe. This solution also won't do, because it create a new console, even if application was run from cmd.

编辑:为了澄清,我想我的应用程序输出到现有控制台(如果有)和以创建一个新的,如果它缺少

to clarify, I want my app to output to the existing console if there is one and not to create a new one if it is missing.

推荐答案

挖了一点后,我发现这个答案。在code现在是:

After digging up a bit, I found this answer. The code is now:

namespace WpfConsoleTest
{
    public partial class App : Application
    {
        [DllImport("Kernel32.dll")]
        public static extern bool AttachConsole(int processId);

        protected override void OnStartup(StartupEventArgs e)
        {
            AttachConsole(-1);
            Console.WriteLine("Start");
            System.Threading.Thread.Sleep(1000);
            Console.WriteLine("Stop");
            Shutdown(0);
        }
    }
}

调用exe文件直接还是有一个讨厌的副作用,与呼叫立即返回连接:

Calling the exe directly still has a nasty side effect, connected with the call returning immediately:

C:\test>WpfConsoleTest.exe

C:\test>Start
Stop

^^^^
The cursor will stay here waiting for the user to press enter!

解决方案是,再一次,使用的启动的:

C:\test>start /wait WpfConsoleTest.exe
Start
Stop

感谢您的输入!

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

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