The Code is as follows:
// Edit the web. config file
// Open the configuration file
Configuration config = System. Web. Configuration. WebConfigurationManager. OpenWebConfiguration ("~ ");
// Obtain the deleteworker Node
AppSettingsSection appsection = (AppSettingsSection) config. GetSection ("appSettings ");
// Add an element to the deleteworker Node
Appsection. Settings. Add ("addkey1", "key1's value ");
Appsection. Settings. Add ("addkey2", "key2's value ");
Config. Save ();
// Delete a node or attribute
// Open the configuration file
Configuration config = System. Web. Configuration. 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 key2's value ";
Config. Save ();
//////////////////////////////////////// ////////////////////////////////
// Edit the App. config file
ExeConfigurationFileMap file = new ExeConfigurationFileMap ();
File. ExeConfigFilename = @ "... \ .. \ test. config ";
// Open the configuration file
Configuration myConfig = System. Configuration. ConfigurationManager. OpenMappedExeConfiguration (file, ConfigurationUserLevel. None );
// Obtain the deleteworker Node
AppSettingsSection appsection = (AppSettingsSection) myConfig. GetSection ("appSettings ");
// Add an element to the deleteworker Node
Appsection. Settings. Add ("addkey1", "key1's value ");
Appsection. Settings. Add ("addkey2", "key2's value ");
Config. Save ();
// Delete a node or attribute
// Open the configuration file
ExeConfigurationFileMap file = new ExeConfigurationFileMap ();
File. ExeConfigFilename = @ "... \ .. \ test. config ";
// Open the configuration file
Configuration myConfig = System. Configuration. ConfigurationManager. OpenMappedExeConfiguration (file, ConfigurationUserLevel. None );
// Obtain the deleteworker Node
AppSettingsSection appsection = (AppSettingsSection) myConfig. 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 key2's value ";
MyConfig. Save ();