BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

System Information
.NET 1.1+

Obtaining the Executed Command Line

In some situations it can be useful to determine the command line that was used to start a program. In these cases it is possible to identify the exact text entered by the user or generated by the operating system to launch the software.

Environment Class

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:

Demo.exe
Demo
demo
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.

Console.WriteLine(Environment.CommandLine);
1 February 2014