tags:

views:

86

answers:

2

hi

how to send 2-3 param's to Winform C# program ?

for example: i'll send something like MyProg.exe 10 20 "abc"

and in my program i can receive those values

(i dont want to show MyProg.exe - it will work background)

thank's in advance

+3  A: 

For that there is the

Main(params string[] args)
{
}

All the arguments you passed to the application are in the string array args. You can read them from there and react correspdonding.

Sebastian P.R. Gingter
+5  A: 

Open up your Program.cs which is the entry point of your application. The main method is the one that fires up your application and this is the entry method.

You need to modify it a bit by chaning:

static void Main() to something else that will allow you to send an array of elements.

Try changing it to:

static void Main(string[] args) and loop through args and see what you get.

You can see a bit more examples and explenations over here: Access Command Line Arguments.

There are good libraries which will help you out a bit to parse these command line arguments aswell.

Examples

To give you a bit more information I put together an example on an alternative way as Kobi mentioned:

class Program
{
    static void Main()
    {
        ParseCommnandLineArguments();
    }

    static void ParseCommnandLineArguments()
    {
        var args = Environment.GetCommandLineArgs();

        foreach(var arg in args)
            Console.WriteLine(arg);
    }
}

CommandLineArguments.exe -q a -b r

will then Output

CommandLineArguments.exe

-q

a

-b

r

The same result would also be possible with this way

class Program
{
    static void Main(string[] args)
    {
        foreach (var arg in args)
            Console.WriteLine(arg);
    }
}
Filip Ekberg
And don't forget `Environment.GetCommandLineArgs()` - you can access the arguments from anywhere, not just `main`.
Kobi
True! That's in the "bit more examples and explenations" link ;)
Filip Ekberg
Why would the four tokens get separated into only two arguments instead of four? All links provided suggest the whitespace is always a delimiter (except when using quotes).
MEMark
@MEMark, whitespace is a delimiter, just wrong formatting when pasting the result into the editor.
Filip Ekberg