C # Use the configurationmanager class to operate the configuration file
If flexible operations and read/write configuration files are required in. net1.1, a configuration file management class is encapsulated in the project for read/write operations. In. net2.0, The configurationmanager and webconfigurationmanager classes can be used to manage configuration files. The configurationmanager class is in system. configuration, and the webconfigurationmanager class is in system. Web. configuration. According to msdn, we recommend that you use
The system. Web. configuration. webconfigurationmanager class, instead of the system. configuration. configurationmanager class.
The following is a simple example to illustrate how to use webconfigurationmanager to operate the configuration file:
// Open the configuration file
Configuration Config = webconfigurationmanager. openwebconfiguration ("~ ");
// Obtain the deleteworker Node
Appsettingssection appsection = (appsettingssection) config. getsection ("appsettings ");
// Add an element to the deleteworker Node
Appsection. settings. Add ("addkey1", "key1s value ");
Appsection. settings. Add ("addkey2", "key2s value ");
Config. Save ();
After running the code, you can see the changes in the configuration file:
<Deleetask>
<Add key = "addkey1" value = "key1s value"/>
<Add key = "addkey2" value = "key2s value"/>
</Appsettings>
It is also convenient to modify and delete nodes or attributes:
// Open the configuration file
Configuration Config = webconfigurationmanager. openwebconfiguration ("~ ");
// Obtain the deleteworker Node
Appsettingssection appsection = (appsettingssection) config. getsection ("appsettings ");
// Delete the elements in the deleteworker Node
Appsection. settings. Remove ("addkey1 ");
// Modify the element in the deleteworker Node
Appsection. settings ["addkey2"]. value = "Modify key2s value ";
Config. Save ();
Configuration file:
<Deleetask>
<Add key = "addkey2" value = "Modify key2s value"/>
</Appsettings>
========================================================== ========================================================
Write the database connection string to Web. config.
Protected void page_load (Object sender, eventargs E)
{
System. Data. sqlclient. sqlconnectionstringbuilder builder = new system. Data. sqlclient. sqlconnectionstringbuilder ();
Builder. datasource = "localhost ";
Builder. initialcatalog = "northwind1 ";
Builder. userid = "sa ";
Builder. Password = "password ";
Builder. persistsecurityinfo = true;
Configuration Config = system. Web. configuration. webconfigurationmanager. openwebconfiguration (request. applicationpath );
// Config. connectionstrings. connectionstrings ["appconnectionstring"]. connectionstring = builder. connectionstring;
Appsettingssection appsection = (appsettingssection) config. getsection ("appsettings ");
Appsection. settings. Add ("connstr", builder. connectionstring );
Config. Save ();
}
If flexible operations and read/write configuration files are required in. net1.1, a configuration file management class is encapsulated in the project for read/write operations. In. net2.0, The configurationmanager and webconfigurationmanager classes can be used to manage configuration files. The configurationmanager class is in system. configuration, and the webconfigurationmanager class is in system. Web. configuration. According to msdn, we recommend that you use
The system. Web. configuration. webconfigurationmanager class, instead of the system. configuration. configurationmanager class.
The following is a simple example to illustrate how to use webconfigurationmanager to operate the configuration file:
// Open the configuration file
Configuration Config = webconfigurationmanager. openwebconfiguration ("~ ");
// Obtain the deleteworker Node
Appsettingssection appsection = (appsettingssection) config. getsection ("appsettings ");
// Add an element to the deleteworker Node
Appsection. settings. Add ("addkey1", "key1s value ");
Appsection. settings. Add ("addkey2", "key2s value ");
Config. Save ();
After running the code, you can see the changes in the configuration file:
<Deleetask>
<Add key = "addkey1" value = "key1s value"/>
<Add key = "addkey2" value = "key2s value"/>
</Appsettings>
It is also convenient to modify and delete nodes or attributes:
// Open the configuration file
Configuration Config = webconfigurationmanager. openwebconfiguration ("~ ");
// Obtain the deleteworker Node
Appsettingssection appsection = (appsettingssection) config. getsection ("appsettings ");
// Delete the elements in the deleteworker Node
Appsection. settings. Remove ("addkey1 ");
// Modify the element in the deleteworker Node
Appsection. settings ["addkey2"]. value = "Modify key2s value ";
Config. Save ();
Configuration file:
<Deleetask>
<Add key = "addkey2" value = "Modify key2s value"/>
</Appsettings>
========================================================== ========================================================
Write the database connection string to Web. config.
Protected void page_load (Object sender, eventargs E)
{
System. Data. sqlclient. sqlconnectionstringbuilder builder = new system. Data. sqlclient. sqlconnectionstringbuilder ();
Builder. datasource = "localhost ";
Builder. initialcatalog = "northwind1 ";
Builder. userid = "sa ";
Builder. Password = "password ";
Builder. persistsecurityinfo = true;
Configuration Config = system. Web. configuration. webconfigurationmanager. openwebconfiguration (request. applicationpath );
// Config. connectionstrings. connectionstrings ["appconnectionstring"]. connectionstring = builder. connectionstring;
Appsettingssection appsection = (appsettingssection) config. getsection ("appsettings ");
Appsection. settings. Add ("connstr", builder. connectionstring );
Config. Save ();
}