Obtain the parameter values in the App. config configuration file.

Source: Internet
Author: User

Obtain the parameter values in the App. config configuration file.

The following code is used as an example. The specific content is as follows:

First add System. Configuration reference
Add parameters to the App. config configuration file

Add App. config
Add parameters to the App. config configuration file
Example:

In this App. config configuration file, I added four parameters. The App. config parameters, like HashTable, are key-value pairs.

<?xml version="1.0" encoding="utf-8" ?><configuration> <appSettings> <add key="theDate" value="2015-07-20 16:25"/> <add key="theName" value="Alice"/> <add key="theType" value="NBA"/> <add key="thePrice" value="12500.00"/> </appSettings></configuration>

  How can I access the parameter values in the App. config configuration file?

Using System; using System. collections. generic; using System. linq; using System. text; using System. configuration; namespace AppConfigDemo {class Program {static void Main (string [] args) {// judge the App. whether the config file contains Key (non-null) if (ConfigurationManager. appSettings. hasKeys () {// cyclically traverse all the Key keys in the configuration file foreach (string s in ConfigurationManager. appSettings) {Console. writeLine (s) ;}} Console. readKey ();}}}

  The code for traversing the Key using the for loop is as follows:


Static void Main (string [] args) {// judge the App. whether the config file contains Key (non-null) if (ConfigurationManager. appSettings. hasKeys () {// cyclically traverse all the Key keys in the configuration file for (int I = 0; I <ConfigurationManager. appSettings. count; I ++) {Console. writeLine (ConfigurationManager. appSettings. getKey (I) ;}} Console. readKey ();}

  Access Value through Key:

Static void Main (string [] args) {// judge the App. whether the config file contains Key (non-null) if (ConfigurationManager. appSettings. hasKeys () {// obtain the Value foreach (string s in ConfigurationManager) of the "theDate" key. appSettings. getValues ("theDate") {Console. writeLine (s) ;}} Console. readKey ();}

  What if you want to obtain the Value set of all keys?

Idea: traverse all the keys and save them in a container (for example, an array). Then, use the Key to match and find the Value.

Static void Main (string [] args) {// judge the App. whether the config file contains Key (non-null) if (ConfigurationManager. appSettings. hasKeys () {List <string> theKeys = new List <string> (); // List of keys saved <string> theValues = new List <string> (); // Save the Value set // traverse all keys and add them to theKeys set foreach (string theKey in ConfigurationManager. appSettings. keys) {theKeys. add (theKey) ;}// traverse all values based on the Key and Add them to the theValues set for (int I = 0; I <theKeys. count; I ++) {foreach (string theValue in ConfigurationManager. appSettings. getValues (theKeys [I]) {theValues. add (theValue) ;}/// verify the Console. writeLine ("************** Key *************"); foreach (string s in theKeys) {Console. writeLine (s);} Console. writeLine ("************* Value ************"); foreach (var item in theValues) {Console. writeLine (item) ;}} Console. readKey ();}


The above Code uses the. net technology to obtain the parameter values in the app. config configuration file. For more information, see.

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.