32

I have a WPF C# application, to which I have to pass command line argument. The argument is actually a URL, which I have to then use in my application?

How are these command line arguments passed in WPF C#, so that the application can pickup the url during launch?

Cipher
  • 5,894
  • 22
  • 76
  • 112
  • 1
    Take a look at http://msdn.microsoft.com/en-us/library/aa972153(v=vs.90).aspx or google for WPF command line arguments – Joe Mar 07 '12 at 11:20
  • 3
    FWIW, Googling for "wpf command line arguments", the top 4 links were all to stackoverflow, including this one, that's how I landed here. – cdkMoose May 06 '14 at 13:07

3 Answers3

59

In your App.xaml.cs

class App : Application
{
    //Add this method override
    protected override void OnStartup(StartupEventArgs e)
    {
        //e.Args is the string[] of command line arguments
    }
}
Jeff
  • 8,020
  • 34
  • 99
  • 157
linquize
  • 19,828
  • 10
  • 59
  • 83
  • So, that means I'll have to do this in App.xaml and not in `MainWindow.xaml` etc. Is it? – Cipher Mar 07 '12 at 11:21
  • 16
    If use in MainWindow.xaml.cs directly, then as suggested by others, Environment.GetCommandLineArgs() – linquize Mar 07 '12 at 11:25
  • Is there some way to pass and check the command line arguments for my application from within Visual Studio, and not trying to lauch the application from outside? – Cipher Mar 07 '12 at 11:32
33

It has been mentioned by linquize above, but I think it is worth an answer of its own, as it is so simple...

You can just use:

string[] args = Environment.GetCommandLineArgs();

That works anywhere in the application, not just in App.xaml.cs

Andreas Kahler
  • 2,283
  • 1
  • 17
  • 17
1

You can pass arguments like "no-wpf" C# applications through comman line. The difference is the application entry point. In WPF is App.xaml.cs. So, you have in this file you can pick arguments in this way:

class App : Application
{
    protected override void OnStartup(StartupEventArgs e)
    {
        //e.Args represent string[] of no-wpf C# applications
    }
}
Angelo Badellino
  • 2,141
  • 2
  • 24
  • 45