The following code samples to show you, the specific content is as follows:
First add System.Configuration Reference
To add parameters to the app.config configuration file
app.config Add
to add parameters to the app.config configuration file
Example:
In this app.config configuration file, I have added 4 parameters, 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" Alice "value=/> <add" key= "Thetype
"NBA"/>
<add key= "Theprice" value= "12500.00"/>
</appSettings>
</configuration >
How do you 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)
{
// Determine if the app.config configuration file has a key (non-null)
if (ConfigurationManager.AppSettings.HasKeys ())
{
// Loops through all the key-
foreach (string s in configurationmanager.appsettings)
{
Console.WriteLine (s) of the configuration file;
}
}
Console.readkey ();}}
The code to traverse the key using a For loop is as follows:
static void Main (string[] args)
{
//determine if a key (not NULL) is in the App.config configuration file (
ConfigurationManager.AppSettings.HasKeys ())
{
//loops through all key keys for
(int i = 0; i < in the configuration file) ConfigurationManager.AppSettings.Count; i++)
{
Console.WriteLine (ConfigurationManager.AppSettings.GetKey (i));
}
Console.readkey ();
}
How to access value via key:
static void Main (string[] args)
{
//determine if a key (not NULL) is in the App.config configuration file (
ConfigurationManager.AppSettings.HasKeys ())
{
//Get the value foreach of the "Thedate" key
(string s in ConfigurationManager.AppSettings.GetValues ("Thedate"))
{
Console.WriteLine (s);
}
}
Console.readkey ();
}
What if you want to get the value collection of all keys?
Idea: All keys are traversed and stored in a container (for example, an array), and then a key match is used to find the value.
static void Main (string[] args) {//Determine if a key (not null) if (Config) is in the app.config configuration file UrationManager.AppSettings.HasKeys ()) {list<string> Thekeys = new list<string> ();//Save Key collection list& lt;string> thevalues = new list<string> ();
Save the collection of value//traverse out all key and add it to Thekeys set foreach (String Thekey in ConfigurationManager.AppSettings.Keys) {
Thekeys.add (Thekey); "//////Thevalues All value according to key and add it to the set for (int i = 0; i < Thekeys.count; i++) {foreach (String thevalue
In ConfigurationManager.AppSettings.GetValues (Thekeys[i])) {Thevalues.add (thevalue);
}//Verify 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 code above is an example of using the. NET technology to get the parameter values in the app.config configuration file, as a friend can refer to.