C # Reading and modifying configuration files (Code tutorial ),
Preface
Configuration files are used in many cases. configuration files are divided into two types: application configuration files and web configuration files, the maximum two configuration files are those that are updated in real time after the web configuration file is updated. The configuration files of the application are not updated in real time, and must be refreshed after the update. (ConfigurationManager needs to be updated, but Configuration does not)
Method
Before calling ConfigurationManager, you must add system. configuration. dll Assembly reference in the project.
Update the configuration file:
Configuration cft = ConfigurationManager. OpenExeconfiguration (ConfigurationUserLevel. None );
// Add
Cft. AppSettings. Settings. Add ("Key", "Name ");
// Modify
Cft .. AppSettings. Settings ["DB"]. Value = "Name ";
// Final call
Cft. Save ();
// The current configuration file is updated successfully.
ConfigurationManager. RefreshSection ("receivettings"); // The application must refresh the named node.