Command line options

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

+4
source share
3 answers

If you do not want to override the main method, you can use the environment class.

foreach (string arg in Environment.GetCommandLineArgs()) { Console.WriteLine(arg); } 
+8
source

Yes, the command line is a good way to pass information to the program. It is available from the Main function of any .Net program.

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

Elsewhere in the program, you can access it by calling Environment.GetCommandLineArgs . Be careful though the command line information may be changed after the program starts. It is just a block of its own memory that can be written by a program

+5
source

The easiest way to read a command line argument in C # is to make sure your Main method accepts the string[] parameter - this parameter is populated with the arguments passed from the command line.

  $ cat a.cs
 class program
 {
     static void Main (string [] args)
     {
         foreach (string arg in args)
         {
             System.Console.WriteLine (arg);
         }
     }
 }
 $ mcs a.cs
 $ mono ./a.exe arg1 foo bar
 arg1
 foo
 bar
+2
source

All Articles