Read/write ASP. NET web. config

Source: Internet
Author: User

If you need to read and write ASP. NET web. config. See system. Web. configuration.

Before writing code, reference system. Data, system. Collection, system. Web. configuration, system. componetmodule.

Write:

Configuration chapter = webconfigurationmanager. openwebconfiguration (request. applicationpath );
If (chapter! = NULL)
{

Appsettingssection cfghandler = chapter. getsection ("appsettings") as appsettingssection;
Confighander handler = new confighander ();
Handler. firstname = "Gary ";
Handler. lastname = "yang ";
Handler. sectioninformation. forcesave = true;
Cfghandler. settings. Add ("Gary", "garyyang ");
Chapter. Save ();
}

If you want to add objects to Web. config, You need to implement the corresponding classes:

This is because. NET Framework provides the iconfigurationsectionhandler interface.

Public class confighander: configurationsection
{
[Configurationproperty ("lastname", isrequired = false, defaultvalue = "notgiven")]
Public String lastname
{
Get {
Return (string) base ["lastname"];
}
Set
{
Base ["lastname"] = value;
}
}
[Configurationproperty ("firstname", isrequired = false, defaultvalue = "notgiven")]
Public String firstname
{
Get
{
Return (string) base ["firstname"];
}
Set
{
Base ["firstname"] = value;
}
}
Public confighander ()
{}
}

 

More information: http://www.cnblogs.com/agassi001/archive/2008/01/02/1023811.html

Custom. NET application configuration section instance (Web. config write your own xml configuration )!
Http://www.cnblogs.com/xiazhi33/articles/945429.html

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.