Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command Line Parameters

Tags:

c#

I want to deploy a piece of software to pcs that will need to be able to tell the program a few pieces of information. I do not want to use a configuration file because the exe will be located on a shared drive and they will not have access to run their own config. Would a command line parameter be the best way to do this? If so, how would I pass this and pick it up inside of a c# program?

like image 993
muncherelli Avatar asked Dec 07 '22 01:12

muncherelli


2 Answers

If you dont want to override the main method, you can use the Environment class.

foreach (string arg in Environment.GetCommandLineArgs())
{
    Console.WriteLine(arg);
}
like image 182
Sam Trost Avatar answered Dec 09 '22 14:12

Sam Trost


Yes the command line is a good way of passing information to a program. It is accessible from the Main function of any .Net program

public static void Main(string[] args) {
   // Args is the command line 
}

From elsewhere in the program you can access it with the call Environment.GetCommandLineArgs. Be warned though that the command line information can be modified once the program starts. It's simply a block of native memory that can be written to by the program

like image 44
JaredPar Avatar answered Dec 09 '22 13:12

JaredPar