Get Command Line Arguments in WPF and C#

To get command line arguments of a WPF application, you need to check command line arguments on Application Startup event handler.

The following code snippet shows Application Startup event handler. As you can see from this code, the second parameter of Startup method is StartupEventArgs. The Args property of StartupEventArgs returns an array of strings that are the command line arguments passed to the application.

private void Application_Startup(object sender, StartupEventArgs e)

{

    // CommandLine Arguments found?

    if (e.Args.Length > 0)

    {

        // Loop through all commandline arguments

        // which is an array of strings

        foreach (string arg in e.Args)

        {

            MessageBox.Show("Arg =" + arg);

        }

    }

}