4

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

stakx - no longer contributing
  • 80,142
  • 18
  • 159
  • 256
pedram
  • 3,417
  • 6
  • 22
  • 28
  • While this is not what you're asking about, you should be aware that this command-line syntax doesn't adhere to any of the usual conventions (e.g. `-x ...` on UNIX-like systems, or `/x ...` on Windows systems) and people will probably have a hard time remembering it. – stakx - no longer contributing Aug 07 '10 at 12:50
  • 5
    Consider writing `-i 100 -k "hello"` etc, instead of `i- 100 k- "hello"` - almost all programs out there use the `-i` style and for good reasons. – Roman Starkov Aug 07 '10 at 12:50

4 Answers4

3

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.

Community
  • 1
  • 1
Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902
2

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

String[] arguments = Environment.GetCommandLineArgs();

More info on MSDN

hemme
  • 1,636
  • 1
  • 15
  • 22
2

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.

Community
  • 1
  • 1
Henk Holterman
  • 250,905
  • 30
  • 306
  • 490
1

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

Timwi
  • 63,217
  • 30
  • 158
  • 225