Method 1 - Replace the Main Method
When a new Windows Forms application is created using Visual Studio, a simple version of the entry point method, Main, is generated automatically. This basic Main method is ideal for most applications but must be adjusted to accept parameters from the command line.
The automatically generated Main method is found in the file, Program.cs and looks like:
To add the ability to accept parameters, we simple add a string array parameter to the Main method definition. For example:
static void Main(string args)
It is then possible to identify the string values passed on start-up by checking the values in the array. If no parameters are passed, the array will be empty. Otherwise, one parameter will appear in each element of the array, with parameters being delimited by spaces.
Method 2 - The Environment Class
A second method for retrieving the application start-up parameters is to use the Environment class. This class, found in the System namespace, includes a static method named GetCommandLineArgs. When executed, an array of strings is returned with the first element of the array containing the file name of the program, if available, and the other elements holding the command line arguments.
string args = Environment.GetCommandLineArgs();
25 July 2006