Using Command-line Parameters In WPF

by jason31. December 2008 01:25

If you need to read command-line parameters within your WPF application, your first inclination (if you are like me) might be to go looking for the Main method. Of course, you won't find a "Main" method in a WPF application. Instead, start looking at the App.xaml file.

By the way, for the rest of this post, I'll presume you will be using command line switches like the following:

myapplication /key1 value1 /key2 value2

To begin, you will need to add an event handler to the App.xaml file for the Startup event. After that, your App.xaml file might look something like this:

<Application x:Class="WpfApplication1.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
StartupUri="Window1.xaml" Startup="AppStartup">
<Application.Resources>
</Application.Resources>
</Application>

(Notice the "Startup" attribute on line 4)

Your event handler's signature (in the App.xaml.cs) should look like this:

void AppStartup(object sender, StartupEventArgs e)
More...

Tags: , , ,

Development | WPF

About

Jason Williams is a .NET developer in Lincoln, Nebraska.

The name "Centrolutions" came out of a long search for a domain name. The goal was to create a name that conveyed an ideology of writing software centered (Centr--) on a solution (--olutions) for a particular problem. In other words, it was the only name in a long list that wasn't already registered on the internet.

If you're looking for the products I have for sale, you should go here.