是否可以拥有WPF应用程序打印控制台输出?

时间:2021-05-29 18:13:52

I have a simple WPF application. Under normal use, App.xaml will launch MainWindow.xaml. But I would like to set it up so that if it is invoked with a special command line argument, that it will function as a console application instead.

我有一个简单的WPF应用程序。在正常使用下,App.xaml将启动MainWindow.xaml。但我想设置它,以便如果使用特殊的命令行参数调用它,它将作为控制台应用程序。

So here's roughly what my App.xaml.cs file looks like:

所以这里大致是我的App.xaml.cs文件的样子:

using System;

namespace MyProject
{
    public partial class App : Application
    {
        public void App_OnStartup(object sender, StartupEventArgs e)
        {
            if (e.Args.Length == 1 && e.Args[0].Equals("/console"))
            {
                Console.WriteLine("this is a test");
                Environment.Exit(0);
            }
            else
            {
                var mainWindow = new MainWindow();
                mainWindow.Show();
            }
        }
    }
}

I would expect that when I run MyProject.exe /console from the command line, it would print the string "this is a test". But right now it doesn't print anything. How can I get it to work?

我希望当我从命令行运行MyProject.exe / console时,它会打印字符串“这是一个测试”。但是现在它没有打印任何东西。我怎样才能让它发挥作用?

2 个解决方案

#1


8  

We have special class for this purpose:

我们为此目的有特殊课程:

internal static class ConsoleAllocator
{
    [DllImport(@"kernel32.dll", SetLastError = true)]
    static extern bool AllocConsole();

    [DllImport(@"kernel32.dll")]
    static extern IntPtr GetConsoleWindow();

    [DllImport(@"user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    const int SwHide = 0;
    const int SwShow = 5;


    public static void ShowConsoleWindow()
    {
        var handle = GetConsoleWindow();

        if (handle == IntPtr.Zero)
        {
            AllocConsole();
        }
        else
        {
            ShowWindow(handle, SwShow);
        }
    }

    public static void HideConsoleWindow()
    {
        var handle = GetConsoleWindow();

        ShowWindow(handle, SwHide);
    }
}

Just call ConsoleAllocator.ShowConsoleWindow() and then write to Console

只需调用ConsoleAllocator.ShowConsoleWindow(),然后写入Console

#2


2  

If you don't want the Application object to be created then you should create a separate class containing a Main entry point:

如果您不希望创建Application对象,那么您应该创建一个包含Main入口点的单独类:

class Startup
{
    [STAThreadAttribute]
    public static int Main(string[] args)
    {
        if (args.Length == 0)
        {
            // run windowed
            App app = new App();
            app.InitializeComponent();
            app.Run();
        }
        else
        {
            // run console
            ConsoleManager.Show();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

        return 0;
    }
}

You'll also need to go into your application settings and change "Startup object" to "YourProjectNamespace.Startup", see this article for more details.

您还需要进入应用程序设置并将“启动对象”更改为“YourProjectNamespace.Startup”,有关详细信息,请参阅此文章。

The console functions I'm calling come from the accepted answer to this question which shows how to tap into the standard Console streams.

我正在调用的控制台功能来自这个问题的接受答案,它显示了如何使用标准的控制台流。

#1


8  

We have special class for this purpose:

我们为此目的有特殊课程:

internal static class ConsoleAllocator
{
    [DllImport(@"kernel32.dll", SetLastError = true)]
    static extern bool AllocConsole();

    [DllImport(@"kernel32.dll")]
    static extern IntPtr GetConsoleWindow();

    [DllImport(@"user32.dll")]
    static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

    const int SwHide = 0;
    const int SwShow = 5;


    public static void ShowConsoleWindow()
    {
        var handle = GetConsoleWindow();

        if (handle == IntPtr.Zero)
        {
            AllocConsole();
        }
        else
        {
            ShowWindow(handle, SwShow);
        }
    }

    public static void HideConsoleWindow()
    {
        var handle = GetConsoleWindow();

        ShowWindow(handle, SwHide);
    }
}

Just call ConsoleAllocator.ShowConsoleWindow() and then write to Console

只需调用ConsoleAllocator.ShowConsoleWindow(),然后写入Console

#2


2  

If you don't want the Application object to be created then you should create a separate class containing a Main entry point:

如果您不希望创建Application对象,那么您应该创建一个包含Main入口点的单独类:

class Startup
{
    [STAThreadAttribute]
    public static int Main(string[] args)
    {
        if (args.Length == 0)
        {
            // run windowed
            App app = new App();
            app.InitializeComponent();
            app.Run();
        }
        else
        {
            // run console
            ConsoleManager.Show();
            Console.WriteLine("Press any key to exit...");
            Console.ReadKey();
        }

        return 0;
    }
}

You'll also need to go into your application settings and change "Startup object" to "YourProjectNamespace.Startup", see this article for more details.

您还需要进入应用程序设置并将“启动对象”更改为“YourProjectNamespace.Startup”,有关详细信息,请参阅此文章。

The console functions I'm calling come from the accepted answer to this question which shows how to tap into the standard Console streams.

我正在调用的控制台功能来自这个问题的接受答案,它显示了如何使用标准的控制台流。