33

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?

2
  • 1
    Take a look at msdn.microsoft.com/en-us/library/aa972153(v=vs.90).aspx or google for WPF command line arguments Commented Mar 7, 2012 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. Commented May 6, 2014 at 13:07

3 Answers 3

60

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
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

So, that means I'll have to do this in App.xaml and not in MainWindow.xaml etc. Is it?
If use in MainWindow.xaml.cs directly, then as suggested by others, Environment.GetCommandLineArgs()
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?
34

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

Comments

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
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.