Ok, this was really bugging me… I needed to figure out how to create a Window application, and parse in the command line arguments. I know how to do it with a Console application, but not a Window app. So, how do you do it?
Well, it’s a little tricky, but it can be done.
First, you need to add an event handler to the Application (App) itself, so go into App.xaml and add an event handler for Startup. The code will now look like this (I added the lines in Italics):
<!– Resources scoped at the Application level should be defined here. –>
Now you need to define that method in your application, and you need a place to put it. So, open App.xaml.cs. Define a string to hold the arguments, and handle the event. Easily enough:
public partial class App: System.Windows.Application
public static string args;
void app_Startup(object sender, StartupEventArgs e)
App.args = e.Args;
If you notice, the app_Startup event handler takes StartupEventArgs, and one of the fields on that ‘e’ object is Args. The command lines are broken up on quotes and then on spaces. So, if I called my app as: MyApp “1 2 3” 4 “5 6”, I would get three arguments. The first is 1 2 3, the second is 4 and the last is 5 6.
Finally, you can now access those args in your Window1.Initialize by just accessing App.args.
foreach (string key in App.args)