First, I will introduce the syntax in the configuration file:
1.Create a config file under the project in VS2005, The name is App. config, and is edited as follows:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<ConfigSections>
<Section
Name = "StartParameters"
Type = "System. Configuration. NameValueSectionHandler, System, Version = 1.0.3300.0, Culture = neutral, PublicKeyToken = b77a5c561934e089"/>
</ConfigSections>
<StartParameters>
<Add key = "IPAddress" value = "127.0.0.1"/>
<Add key = "Port" value = "13000"/>
</StartParameters>
</Configuration>
The name value of the section node is defined by myself. Here I define it as "StartParameters", and then add the declared node above, add two test items "<add key =" IPAddress "value =" 127.0.0.1 "/>" and "<add key =" Port "value =" 13000 "/> "; the configuration file is defined.
2. Open the code file to read the configuration information and add two references:
Copy codeThe Code is as follows:
Using System. Configuration;
Using System. Collections. Specialized;
Defines a NameValueCollection type variable:
Copy codeThe Code is as follows:
NameValueCollection _ table = null;
_ Table = (NameValueCollection) ConfigurationManager. GetSection ("StartParameters ");
String ipAddress = _ table ["IPAddress"]. ToString ();
String port = _ table ["Port"]. ToString ();
In the above sentence, "StartParameters" is the name value defined in the configuration file.
The output ipAddress and port values are:
Copy codeThe Code is as follows:
"127.0.0.1"
13000"