I am writing a .NET class that needs to parse the command line of the process. I don't want to have a dependency between the Main() method and that class. How can the class access the command line?
5 Answers
If you use .NET Compact Framework, Environment.GetCommandLineArgs() method isn't implemented and System.Diagnostics.Process.GetCurrentProcess().StartInfo.Arguments returns always empty string, so you must use main function and pass arguments to your others classes.
An example :
[MTAThread]
static void Main(String[] commandLineArguments)
{
CommandLineHelper.parse(commandLineArguments);
}
public static class CommandLineHelper
{
public static void parse(String[] commandLineArguments) {
// add your code here
}
}

- 3,260
- 2
- 30
- 35
-
Thanks for the Compact Framework heads up. In my case I did not want to show the main form when args were specified, so that the exe would run in silent mode, but as you have suggested you could store those args on the static class that would be available to all forms of the application. This should workin any framework. For those interested here was my code. [MTAThread] static void Main(String[] cmd) { if (cmd.Length == 0) Application.Run(new frmMain()); else MessageBox.Show(cmd[0]); } – kuklei Feb 02 '17 at 13:24
String[] myStr = Environment.GetCommandLineArgs();
its always good to complete the example.

- 41
- 1
System.Diagnostics.Process.GetCurrentProcess().StartInfo.Arguments

- 26,356
- 27
- 122
- 180

- 58,735
- 39
- 131
- 204
-
Actually, this does not seem to work. It seems always empty. See http://stackoverflow.com/questions/344056/why-is-startinfo-processstartinfo-always-empty – Carsten Jul 15 '13 at 00:12
Create a class that holds your application's options. In the main method create an instance of that class, initialise it with the command line arguments, and pass it in to the classes that need it.
Alternatively, you could have the class initialised at any time thereafter by having it created via a CustomConfigClass.Create()
method that uses Environment.GetCommandLineArgs()
.
The first option would be my recommendation, because it makes the class easier to prepare for unit testing and switching to an alternative method of configuration at a later date without breaking the application due to a dependency on the command line.

- 26,356
- 27
- 122
- 180

- 41,080
- 29
- 148
- 220
-
The first option still requires the main method to know about the other class, so it does not solve my problem. I'm using an IOC container, therefore I will create a service that exposes the GetCommandLineArgs functionality, and make my other service depend from it. Thanks. – Antoine Aubry Apr 12 '09 at 23:41
-
1That misses my point slightly, though. If you're using IoC, then don't have a service to return the command line args, have a service interface that returns the config, and implement that with a class that gets it from the command line. That way you can replace with another class for unit testing. – Neil Barnwell Apr 13 '09 at 14:58