Get Arguments array from CmdLine of a WPF Application.

By | December 18, 2009


This is my first post of, I hope, a series related to WPF and C#.

The examples below are made with VS (Visual Studio) 2010 and .Net 4.0, for this precise example you can make it work with .Net 3.5 at least.

During the life of an application we can listen for some important events, these events can help us to drive the logic of our application from the startup to the end of an application life.

If you are creating a simple WPF Application in VS you will get a file called App.xaml for free. In this file you can add arguments to handle events.

<Application x:Class=”WpfAppStartup.App”










As you can see in the above code, I added the Startup and Exit arguments in the Application node.
(Tip: just write ‘Startup=’ and then press Tab on your keyboard to have the method name and signature automatically created in App.xaml.cs file).

Below is the list of Events you can catch and a short description.




Called when Application.Run() is called, shortly before the main window is displayed. This is where you can extract all cmdline arguments.


Called when the application is shut down, shortly before the Run() methods returns.


Called when application get focus.


Called when application lose focus.


Occurs when the Windows session is ending (log off or shut down computer).


Called when an Unhandled application occurs in your application (main thread).


Now back to our first goal: getting the arguments array!

private void Application_Startup(object sender, StartupEventArgs e)


if (e.Args.Length > 0)


             foreach (string arg in e.Args)


                    // logic for arguments processing.





             // No arguments!



MessageBox.Show(“Application.Run() is called, “ +

“this happen before the main window is displayed.”);


If there are any unclear sentence, please feel free to comment!

0 Kudos

One thought on “Get Arguments array from CmdLine of a WPF Application.