简体   繁体   中英

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?

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM