Wpf and command line application in one executable file

I would like to have one executable file that I can use to open a graphical application (the default use case when clicking on .exe) or that I can use to perform command line tasks.

Is it possible?

If so, how will I need to modify the app.xaml / app.xaml.cs application so that it only displays a graphical representation of certain conditions (for example, no command-line options)?

+5
source share
3 answers

You must first use the WPF application project and modify app.xml 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 missing.

Then on your App.xaml.cs you can do something like this:

 public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); if (condition) { var window = new MainWindow(); window.ShowDialog(); } else { AllocConsole(); } } [DllImport("Kernel32.dll")] static extern void AllocConsole(); } 
+5
source

You can check if the application is running from the console. If not, you can dynamically distribute the console:

 if (GetConsoleWindow() == IntPtr.Zero) AllocConsole(); 

Where

 [DllImport("kernel32.dll")] public static extern IntPtr GetConsoleWindow(); [DllImport("kernel32.dll")] public static extern bool AllocConsole(); 
+2
source

@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.

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)) { // Instantiate view, call View.Show() } else { // Process the 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.

+2
source

All Articles