Metah - A Web Technologist Adventure

Tweets

Get Arguments array from CmdLine of a WPF Application.

December 18th, 2009 at 6:24 pm by Ahmet Gyger

Tags: ,
Posted in CSharp, Microsoft, Tutorials



 

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”

             xmlns=”http://schemas.microsoft.com/winfx/2006/xaml/presentation”

             xmlns:x=”http://schemas.microsoft.com/winfx/2006/xaml”

             Startup=”Application_Startup”

             Exit=”Application_Exit”

             Activated=”Application_Activated”

             Deactivated=”Application_Deactivated”

             SessionEnding=”Application_SessionEnding”

             StartupUri=”MainWindow.xaml”>

 

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.

Name

Description

Startup

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

Exit

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

Activated

Called when application get focus.

Deactivated

Called when application lose focus.

SessionEnding

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

DispatcherUnhandledException

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.

              }

}

       else

       {

             // 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!

One Response to “Get Arguments array from CmdLine of a WPF Application.”

  1. simi
    December 30th, 2009 08:57
    1

    hi,

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful for .Net community.

    There is a good C# resource site, Have alook

    http://www.csharptalk.com/2009/09/c-array.html
    http://www.csharptalk.com/2009/10/creating-arrays.html

    simi

Leave a Reply