views:

77

answers:

3

Hi

How can I find this information :

think we started this process :

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

Now how can I get main argument when application started 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;

regards

+1  A: 

The arguments are passed to the Main function that is being 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"
    // ...
}

Arguments are in the same order as passed in the command line. If you want to use named arguments you may take a look at this post which suggests NDesk.Options and Mono.Options.

Darin Dimitrov
Right, except that the array is zero based, not one based.
Guffa
Yeah - some environments pass the exe name in the 0th argument, but fortunately this stupid convention is gone from .NET.
romkyns
A: 

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

Timwi
A: 

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

String[] arguments = Environment.GetCommandLineArgs();

More info on MSDN

Hemme