是否可以有 WPF 应用程序打印控制台输出?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31978826/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to have a WPF application print console output?
提问by soapergem
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 /consolefrom 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,它会打印字符串“这是一个测试”。但现在它不打印任何东西。我怎样才能让它工作?
回答by Backs
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()然后写入控制台
回答by Mark Feldman
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 articlefor more details.
您还需要进入应用程序设置并将“启动对象”更改为“YourProjectNamespace.Startup”,有关更多详细信息,请参阅此文章。
The console functions I'm calling come from the accepted answer to this questionwhich shows how to tap into the standard Console streams.

