我有一个简单的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"。但现在它没有输出任何内容。我该怎么做才能让它工作?