Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

winforms

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

like image 874
Gold Avatar asked Dec 22 '22 03:12

Gold


1 Answers

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);
    }
}
like image 126
Filip Ekberg Avatar answered Jan 01 '23 20:01

Filip Ekberg