. NET: how to obtain the parameter values in the App. config configuration file,. netapp. config
- First add System. Configuration reference
- Class file must contain using System. Configuration;
- 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.
The Code is as follows:
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 ();}