The configuration file is the basis and basis for the program itself. It is essentially an xml file. The operations on the configuration file are as follows. NET 2.0 is very convenient, and provides the System [. web]. to use the NameSpace of the Configuration management function, you need to add. configuration. dll reference.
For the WINFORM program, use System. Configuration. ConfigurationManager;
For ASP. NET programs, use System. Web. Configuration. WebConfigurationManager;
Reading configuration file content is too common. If your program does not read the content of the configuration file, you are embarrassed to use it.
Let's take the most common etettings section as an example:
Assume that the configuration file contains the following content:
Copy codeThe Code is as follows: <? Xml version = "1.0" encoding = "UTF-8"?>
<Configuration>
<Deleetask>
<Add key = "y" value = "this is Y"/>
</AppSettings>
</Configuration>
1. Read value:
* Asp. Net: System. Web. Configuration. WebConfigurationManager. deleettings ["y"];
* WinForm: System. Configuration. ConfigurationManager. etettings ["y"];
2. Add an item
ASP. NET (requires write permission ):
Configuration config = WebConfigurationManager. OpenWebConfiguration (null );
AppSettingsSection app = config. AppSettings;
App. Settings. Add ("x", "this is X ");
Config. Save (ConfigurationSaveMode. Modified );
WinForm:
Configuration config = ConfigurationManager. OpenExeConfiguration (ConfigurationUserLevel. None );
AppSettingsSection app = config. AppSettings;
App. Settings. Add ("x", "this is X ");
Config. Save (ConfigurationSaveMode. Modified );
3. modify one item
* Asp. Net
Configuration config = WebConfigurationManager. OpenWebConfiguration (null );
AppSettingsSection app = config. AppSettings;
// App. Settings. Add ("x", "this is X ");
App. Settings ["x"]. Value = "this is not Y ";
Config. Save (ConfigurationSaveMode. Modified );
* WinForm
Configuration config = ConfigurationManager. OpenExeConfiguration (ConfigurationUserLevel. None );
AppSettingsSection app = config. AppSettings;
// App. Settings. Add ("x", "this is X ");
App. Settings ["x"]. Value = "this is not Y ";
Config. Save (ConfigurationSaveMode. Modified );
4. delete an item
* Asp. Net
Configuration config = WebConfigurationManager. OpenWebConfiguration (null );
AppSettingsSection app = config. AppSettings;
App. Settings. Remove ("x ");
Config. Save (ConfigurationSaveMode. Modified );
* WinForm
Configuration config = ConfigurationManager. OpenExeConfiguration (ConfigurationUserLevel. None );
AppSettingsSection app = config. AppSettings;
App. Settings. Remove ("x ");
Config. Save (ConfigurationSaveMode. Modified );