views:

127

answers:

7

So I want other users to be able to run my programm sending arguments. how to do such thing?

+2  A: 

You can read command line arguments from Main's optional string[] parameter:

static void Main(string[] args)
{
    if (args.Length >= 1)
    {
        string x = args[0];
        // etc...
    }
}

Note that the following declaration for the Main method is also valid, but then you don't have access to the command line arguments:

static void Main()
{
    // ...
}

See the documentation for more details.

Mark Byers
+2  A: 

This is supported by default, and the arguments will appear in the args array passed to your program.

public static void Main(string[] args)

If you say

App.exe Hello World What's Up

On a command line, you will receive an args array like this:

[0] = "Hello"
[1] = "World"
[2] = "What's"
[3] = "Up"

It's just up to you to determine what arguments you want, how they will be formatted, etc.

Tejs
+4  A: 

The program is run from a method with this signature

public static void Main(string[] args)

The parameter args will contain the command line arguments, split on space.

Tomas Lycken
is it split on space, or is it more complicated than that? if the program where invoked like this: `myapp.exe "blah blah2"`, wouldn't `args[0] == "blah blah2"`?
rmeador
@rmeador this is determined by your shell (generally cmd.exe). You are correct that if you want a space in your argument that you need to quote it. Using another shell (such as bash, common in the unix world, or with cygwin on a PC) you can also escape it with a ` \ ` such as `./mypapp.exe blah\ blah2`
Dolphin
@Dolphin Actually, I think you can escape spaces with backslash on windows (cmd) too. I'm on Unix now, however, so unfortunately I'm not able to test it.
Tomas Lycken
+8  A: 

You mean args when launching? such as myapp.exe blah blah2 blah3

Make your main method look like this:

public static void Main(string[] args)
{

}

now args is an array of the arguments passed into the program. So in the example case, args[0] == "blah", args[1] == "blah2", etc

Joel
@OP: Just out curiosity... what did you think that `string[] args` in all your programs was used for exactly?
Mark
In C#, the string[] args is optional. Maybe he never saw/used it before now?
Joel
+2  A: 

try these:

http://sourceforge.net/projects/csharpoptparse/

http://www.codeproject.com/KB/recipes/command_line.aspx

they basically allow you to define args and parse them in an OO way rather than having to lots of string comparisons and stuff like that. i used a similar one for java and it was great

sweeney
+8  A: 

If you have a Main method (which you'll have with a command-line app) you can access them directly as the args string-array parameter.

public static void Main(string[] args) {
   var arg1 = args[0];
   var arg2 = args[1];
}

If you're some other place in your code you can access the static Environment.GetCommandLineArgs method

//somewhere in your code
var args = Environment.GetCommandLineArgs();
var arg1 = args[0];
var arg2 = args[1];
Tomas
+1 for `Environment.GetCommandLineArgs(); `
Dolphin
+3  A: 

While string[] args works just fine, it's worth mentioning Environment.GetCommandLineArgs.

Jeffrey Knight
+1 I just learned something! Thanks! =)
Will Marcouiller
+1 Here too. I was wondering about that when I wrote my answer.
Mark Byers