Cipher
Cipher

Reputation: 6092

Command line arguments in C# application

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?

Upvotes: 33

Views: 28614

Answers (3)

linquize
linquize

Reputation: 20396

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

Upvotes: 60

Andreas Kahler
Andreas Kahler

Reputation: 2353

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

Upvotes: 34

Angelo Badellino
Angelo Badellino

Reputation: 2201

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

Upvotes: 1

Related Questions