The Environment class is a standard type provided by the .NET framework that gives you access to information about the current operating environment. Some of the methods and properties of the class provide read-only information, whilst others allow you to modify system settings.
A potentially useful static property of the Environment class is CommandLine. This returns a string containing the command line that was used to launch the program. If the software was started using a shortcut, the value includes the command text from the shortcut. When software is started from a command prompt, the property returns a value that includes the typed text, including any configuration switches and paths, and using the capitalisation entered by the user. For example, the following four command lines could be returned for the same program:
c:\my folder\demo /help
You can use this property to read the command line arguments used when starting the software. However, there are better ways to do this, such as reading the Main method's args parameter or calling the Environment.GetCommandLineArgs method.
To demonstrate the property, create a new console application and add the following code to the Main method. This displays the command line when run.
1 February 2014