Add, delete, and modify the application configuration file app. config/web. config in ASP. NET (C #)

Source: Internet
Author: User

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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.