How to analyze the main arguments?

How can I find this information:

We think we started this process:

testFile.exe i- 100 k- "hello" j-"C:\" "D:\Images" f- "true" 

Now, how can I get the main argument when the application starts, so I have:

 int i = ... ; //i will be 100 string k = ... ; // k = hello string[] path = ... ; // = path[0] = "C:\" , path[1] = "D:\Images" bool f = ... ; // f = true; 

considers

+4
source share
4 answers

Arguments are passed to the Main function, which is called:

 static void Main(string[] args) { // The args array contain all the arguments being passed: // args[0] = "i-" // args[1] = "100" // args[2] = "k-" // args[3] = "hello" // ... } 

The arguments are in the same order as on the command line. If you want to use named arguments, you can take a look at this post , which offers NDesk.Options and Mono.Options .

+3
source

You can use Environment.CommandLine or Environment.GetCommandLineArgs ()

 String[] arguments = Environment.GetCommandLineArgs(); 

Additional MSDN Information

+2
source

As already mentioned, you can use the string[] args or Environment.GetCommandLineArgs() parameter. Please note that for CLickOnce deployed applications, you need something else.

You can do your own processing on string[] or use a library, like this one for CodePlex.

For some complicated details about spaces in file names and screens, see this SO question .

+2
source

You can use NDesk.Options . Here is their documentation .

+1
source

All Articles