C # NameValueCollection class reads configuration information

Source: Internet
Author: User

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"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.