Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive an argument in console program?

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

like image 993
Rella Avatar asked Nov 27 '22 12:11

Rella


2 Answers

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];
like image 176
Tomas Avatar answered Dec 04 '22 15:12

Tomas


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

like image 34
Joel Avatar answered Dec 04 '22 13:12

Joel