If flexible operations and read/write operations are required in. net1.1
Configuration File It is not very convenient. Generally,
Configuration File Management class. In. net2.0, The configurationmanager and webconfigurationmanager classes can be well managed.
Configuration File The configurationmanager class is in system. configuration, and webconfigurationmanager is in system. Web. configuration. According to msdn Program We recommend that you use the system. Web. configuration. webconfigurationmanager class instead of the system. configuration. configurationmanager class.
The following is a simple example of how to use webconfigurationmanager.Configuration File:
// OpenConfiguration 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 ();
RunCodeYou can seeConfiguration FileChanges in:
<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:
// OpenConfiguration 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>
I write my own functions to read and write configuration files in the program. It is easier than above.
Private void addcheci (string Group)
{
Configuration Config = system. Web. configuration. webconfigurationmanager. openwebconfiguration ("~ ");
Appsettingssection appset = config. appsettings;
Appset. settings ["listhave"]. Value + = (appset. settings ["listhave"]. value = NULL )? GROUP: "," + group;
Config. Save ();
}
Use environment. Incomplete.