是否可以让 WPF 应用程序打印控制台输出?
Is it possible to have a WPF application print console output?
我有一个简单的 WPF 应用程序。在正常使用情况下,App.xaml 将启动 MainWindow.xaml。但我想对其进行设置,以便如果使用特殊的命令行参数调用它,它将作为控制台应用程序运行。
我的 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();
}
}
}
}
我希望当我从命令行 运行 MyProject.exe /console
时,它会打印字符串 "this is a test"。但现在它不打印任何东西。我怎样才能让它工作?
为此,我们有特殊的 class:
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);
}
}
只需调用 ConsoleAllocator.ShowConsoleWindow()
然后写入控制台
如果您不想创建 Application 对象,那么您应该创建一个单独的 class 包含 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;
}
}
您还需要进入您的应用程序设置并将 "Startup object" 更改为 "YourProjectNamespace.Startup",请参阅 this article 了解更多详情。
我正在调用的控制台函数来自 this question 的已接受答案,其中显示了如何利用标准控制台流。
我有一个简单的 WPF 应用程序。在正常使用情况下,App.xaml 将启动 MainWindow.xaml。但我想对其进行设置,以便如果使用特殊的命令行参数调用它,它将作为控制台应用程序运行。
我的 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();
}
}
}
}
我希望当我从命令行 运行 MyProject.exe /console
时,它会打印字符串 "this is a test"。但现在它不打印任何东西。我怎样才能让它工作?
为此,我们有特殊的 class:
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);
}
}
只需调用 ConsoleAllocator.ShowConsoleWindow()
然后写入控制台
如果您不想创建 Application 对象,那么您应该创建一个单独的 class 包含 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;
}
}
您还需要进入您的应用程序设置并将 "Startup object" 更改为 "YourProjectNamespace.Startup",请参阅 this article 了解更多详情。
我正在调用的控制台函数来自 this question 的已接受答案,其中显示了如何利用标准控制台流。