pedram
pedram

Reputation: 3767

how to parse main arguments?

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

Upvotes: 5

Views: 9048

Answers (4)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038750

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.

Upvotes: 3

Henk Holterman
Henk Holterman

Reputation: 273219

As already answered, you can use the string[] args parameter or Environment.GetCommandLineArgs(). Note that for CLickOnce deployed apps you need something else.

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

For some tricky details on spaces in filenames and escaping quotes, see this SO question.

Upvotes: 2

hemme
hemme

Reputation: 1652

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

String[] arguments = Environment.GetCommandLineArgs();

More info on MSDN

Upvotes: 2

Timwi
Timwi

Reputation: 66573

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

Upvotes: 1

Related Questions