开发者

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

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 wi开发者_如何学编程ll work background)

thank's in advance


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);
    }
}


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜