29

背景:我正在努力向现有的 WPF Windows Application添加命令行和批处理功能。当我在启动时检测到一些选项时,我会抑制窗口出现,进行一些处理并立即退出。现在,因为没有 UI,我想将一些消息输出到 stdout/stderr。考虑以下代码:

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

当我从命令行运行时,我希望得到以下输出:

Start
Stop

但反而:

C:\test>WpfConsoleTest.exe

C:\test>

但是,您可以重定向输出:

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

C:\test>type out.txt
Start
Stop

不幸的是,重定向到 CON 不起作用:

C:\test>WpfConsoleTest.exe > CON

C:\test>

另一个问题是WpfConsoleTest.exe在启动后立即退出。所以:

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

C:\test>

但:

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

到目前为止,我能想到的最好的解决方案是使用start /B /wait

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

这种方法大部分都可以——如果你把它包装在一个蝙蝠中,你可以保留错误代码等等。一个巨大的缺点是您在应用程序结束后获得输出,即您无法跟踪进度,您必须等待正在发生的事情完成。

因此,我的问题是:如何从 WPF Windows 应用程序输出到父控制台?另外,为什么从 WPF 中获取 stdout/stderr 这么难?

我知道我可以在项目设置中将应用程序类型更改为控制台应用程序,但这有一个令人讨厌的副作用 - 控制台窗口始终可见,即使您只是双击 exe。这个解决方案也不行,因为它会创建一个新的控制台,即使应用程序是从 cmd 运行的。

编辑:澄清一下,如果有一个控制台,我希望我的应用程序输出到现有控制台,如果它丢失,则不要创建一个新控制台。

4

4 回答 4

27

经过一番挖掘,我找到了这个答案。现在的代码是:

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 仍然有一个讨厌的副作用,与调用立即返回有关:

C:\test>WpfConsoleTest.exe

C:\test>Start
Stop

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

解决方案是再次使用start

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

感谢您的输入!

于 2012-07-21T10:01:47.940 回答
17

我过去所做的是使其成为控制台应用程序,并在显示 WPF 窗口后调用 P/Invoke 隐藏应用程序的关联控制台窗口:

//added using statements per comments...
using System.Diagnostics;
using System.Runtime.InteropServices;

    internal sealed class Win32
    {
        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        public static void HideConsoleWindow()
        {
            IntPtr hWnd = Process.GetCurrentProcess().MainWindowHandle;

            if (hWnd != IntPtr.Zero)
            {
                ShowWindow(hWnd, 0); // 0 = SW_HIDE
            }
        }
    }
于 2012-05-02T14:48:37.850 回答
8

WPF 应用程序默认没有控制台,但您可以轻松地为其创建一个控制台,然后像在控制台应用程序中一样对其进行写入。我以前使用过这个助手类:

public class ConsoleHelper
{
    /// <summary>
    /// Allocates a new console for current process.
    /// </summary>
    [DllImport("kernel32.dll")]
    public static extern Boolean AllocConsole();

    /// <summary>
    /// Frees the console.
    /// </summary>
    [DllImport("kernel32.dll")]
    public static extern Boolean FreeConsole();
}

要在您的示例中使用它,您应该能够这样做:

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

现在,如果您从命令行运行它,您应该会看到“开始”和“停止”写入控制台。

于 2012-05-02T14:42:20.823 回答
-1

在项目属性中,您可以将项目类型从 Windows 应用程序更改为控制台应用程序,AFAIK 唯一的区别是您可以在应用程序的整个生命周期中使用控制台。不过,我还没有用 WPF 尝试过。

如果要打开和关闭控制台,则应使用Alloc/FreeConsole,但更改项目类型通常更简单。

于 2012-05-02T14:47:44.407 回答