4

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?

3 Answers 3

8

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

foreach (string arg in Environment.GetCommandLineArgs())
{
    Console.WriteLine(arg);
}
Sign up to request clarification or add additional context in comments.

1 Comment

The main difference between getting the args from main and this is that the 0-th element is the name of the executable, from Environment.
5

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

Comments

2

The simplest way to read a command line argument in C# is to make sure your Main method takes a string[] parameter -- that parameter is filled 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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.