@BrunoKlein's answer will work, and I based my answer on its solution. Quoting @BrunoKlein,
First you need to use the WPF application project and modify the app.xaml file so that you can override the window creation.
<Application x:Class="WpfApplication1.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WpfApplication1"> <Application.Resources> </Application.Resources> </Application>
Note that this property of StartupUri is missing.
Now, even easier (this works in Visual Studio 2015 at least), go to the project properties and change the type of output from the Windows application to the console application. This forces the project to build as a console application, but still has the capabilities of a Windows application.
(The class library is highlighted in this photo, select Console application)
You did it! Done.
Now, instead of void Main(string[] args) , your "main" method is the OnStautup method of your automatically generated App class:
public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); string[] args = e.Args; if (SomeConditionBasedOnTheArgs(args)) {
Please note that the difference between this answer and @BrunoKlein's answer is that it will always “show” the console if it starts from the explorer / start menu / desktop, but if you start from the command line, it will start and run all its standard output to this console, like any regular console application.
source share