In many cases, we need to pass parameters to the Windows Forms program when it starts by using a command line or a shortcut. These parameters may be used to load a document, or an initialization configuration file for an application. Especially for large programs that require a highly customized configuration, it is often necessary to adjust the operating parameters to help the user get different running results.
In general, there are two ways we can achieve this requirement:
- Overloaded entry point function (Main)
- Using the Environment class
Overloaded entry point function (Main)
When we create a Windows Forms program in Visual Studio, VS automatically helps us create a default entry point method,--main.
[stathread]static void Main () { application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); Application.Run (New Form1 ());}
This entry-point function basically applies to all programs, but if you want to receive command-line arguments, you must replace it with a main method with parameters. We can simply add a string array parameter to the main method, such as:
[stathread]static void Main (string[] args) { application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); Application.Run (New Form1 ());}
Now we can get the parameter information passed in when the program starts by using the value of the string array (args). If no arguments are passed, the array is empty. Otherwise, each parameter corresponds to an element in the array, which is delimited by whitespace.
Using the Environment class
The second way to get the application startup parameters is to use the environment class. We can find the environment class under the System namespace, with a method called GetCommandLineArgs () in the class. This method returns a string array, and the first value of the array is the file name of the program (which is also the difference between the parameters obtained by the two methods). If available, other elements are arguments passed through the command line.
Note: The command line can be configured with shortcuts such as:
Sample Program
In order to demonstrate the use of these two methods to pass the startup parameters to the Windows Forms program separately, I explained it through a simple program.
- Create a Windows Form application.
- Create a new two form, add a RichTextBox control to it, and then set the Dock property of the RichTextBox control to DockStyle.Fill.
- The
- modifies the entry point function in Program.cs as follows:
[stathread]static void Main (string[] args) { Application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (FALSE); Form form;
//If the first argument is Form1, the program opens the Form1 form, or the Form2 form opens. if (args! = null && args. Length > 0 && args[0] = = "Form1") {form = new Form1 (); ((Form1) Form). commandarguments = args; } else {form = new Form2 (); ((Form2) Form). Commandarguments = System.Environment.GetCommandLineArgs (); } application.run (form);}
- Then, when the two form is loaded, the passed parameters are read and then displayed in the RichTextBox. Here is the code for FORM1:
Public partial class form1:form{public string[] commandarguments {get; set;} Public Form1 () { InitializeComponent (); } private void Form1_Load (object sender, EventArgs e) { if (commandarguments! = null) { foreach (var item i n commandarguments) { richtextbox1.appendtext (item); Richtextbox1.appendtext (Environment.NewLine);}}}
- There are two ways to set parameters, one is through a shortcut, and the other is passed when the EXE file is executed from the command line. The results are the same for both methods, as follows:
- Code Download: Http://pan.baidu.com/s/1i3xb74T
Reference documents
- Windows Forms application Start-up Parameters