asp.net (C #) application configuration file app.config/web.config Add, delete, change operation _ practical skills

Source: Internet
Author: User
The configuration file, for the program itself, is the foundation and the basis, its essence is an XML file, for configuration file operations, starting from. NET 2.0, is very convenient, providing System [. WEB]. Configuration the namespace of this management function, to use it, you need to add a reference to System.configuration.dll.
For WinForm procedures, use System.Configuration.ConfigurationManager;
For ASP.net procedures, use System.Web.Configuration.WebConfigurationManager;
It's too common to read the contents of a configuration file, but if your program doesn't read the contents of the configuration file, you're embarrassed to take it.
We use the most common AppSettings section as an example:
Assume the following configuration file contents:
Copy Code code as follows:

<?xml version= "1.0" encoding= "Utf-8"?>
<configuration>
<appSettings>
<add key= "y" value= "This is Y"/>
</appSettings>
</configuration>

1. Read the value:
* asp.net:system.web.configuration.webconfigurationmanager.appsettings["y"];
* winform:system.configuration.configurationmanager.appsettings["y"];
2. Add a
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 a
* asp.net
Configuration config = webconfigurationmanager.openwebconfiguration (null);
Appsettingssection app = config. AppSettings;
App. Settings.add ("x", "This is X");
App. settings["X"]. Value = "This are 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 are 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);

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.