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