Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line arguments in C# application

Tags:

c#

wpf

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?

like image 340
Cipher Avatar asked Mar 07 '12 11:03

Cipher


People also ask

What are the command line arguments in C?

What are Command Line Arguments in C? Command line arguments are the arguments which the user gives from the operating system's command line during the time of execution. Earlier, we used main() functions without arguments. These command line arguments are handled by the main() function.

What is a command line argument give examples?

A command-line argument is nothing but the information that we pass after typing the name of the Java program during the program execution. These arguments get stored as Strings in a String array that is passed to the main() function. We can use these command-line arguments as input in our Java program.

What is command line argument syntax?

Properties of Command Line Arguments: They are passed to main() function. They are parameters/arguments supplied to the program when it is invoked. They are used to control program from outside instead of hard coding those values inside the code. argv[argc] is a NULL pointer. argv[0] holds the name of the program.


2 Answers

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
    }
}
like image 66
linquize Avatar answered Oct 21 '22 20:10

linquize


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

like image 32
Andreas Kahler Avatar answered Oct 21 '22 21:10

Andreas Kahler