Use programs to dynamically manage web. config files

Source: Internet
Author: User

The Web. config file assumes the following configuration information needs to be managed:

<Deleetask>
<Add key = "sitetitle" value = "site name"/>
<Add key = "siteurl" value = "home site"/>
<Add key = "sitelogo" value = "site logo"/>
<Add key = "sitebanner" value = "site Banner"/>
<Add key = "siteemail" value = "contact email"/>
</Appsettings>

Implemented C # CoreCode:

1. Read related information in Web. config into textbox

Private   Void Page_load ( Object Sender, system. eventargs E)
{
If ( ! Page. ispostback)
{
// Enter the related values in Web. config in textbox.
This . Txttitle. Text = System. configuration. configurationsettings. deleettings [ " Sitetitle " ];
This . Txturl. Text = System. configuration. configurationsettings. deleettings [ " Siteurl " ];
This . Txtlogo. Text = System. configuration. configurationsettings. deleettings [ " Sitelogo " ];
This . Txtbanner. Text = System. configuration. configurationsettings. deleettings [ " Sitebanner " ];
This . Txtemail. Text = System. configuration. configurationsettings. deleettings [ " Siteemail " ];
}

}

2. Write the modified content to Web. config

Private   Void Btnsave_click ( Object Sender, system. eventargs E)
{
String Filename = Server. mappath ( " Web. config " );
String Keyname; // Key name

Xmldocument xmldoc =   New Xmldocument ();
Try
{
Xmldoc. Load (filename );
}
Catch
{
Response. Write ("<SCRIPT> alert ('file reading error. Check whether the Web. config file exists! ') </SCRIPT>");
Return;
}

Xmlnodelist docdnodenamearr = Xmldoc. documentelement. childnodes; // Document node name Array
Foreach (Xmlelement docxmlelement In Docdnodenamearr)
{
If (Docxmlelement. Name. tolower () = " Appsettings " ) // Find the node named "deleettings"
{
Xmlnodelist keynamearr = Docxmlelement. childnodes; // Array of subnode names
If (Keynamearr. Count > 0 )
{
Foreach (Xmlelement In Keynamearr)
{
Keyname = Xmlelement. attributes [ " Key " ]. Innerxml; // Key Value
Switch (Keyname)
{
Case   " Sitetitle " :
Xmlelement. attributes [ " Value " ]. Value = This . Txttitle. text;
Break ;
Case   " Siteurl " :
Xmlelement. attributes [ " Value " ]. Value = This . Txturl. text;
Break ;
Case   " Sitelogo " :
Xmlelement. attributes [ " Value " ]. Value = This . Txtlogo. text;
Break ;
Case   " Sitebanner " :
Xmlelement. attributes [ " Value " ]. Value = This . Txtbanner. text;
Break ;
Case   " Siteemail " :
Xmlelement. attributes [ " Value " ]. Value = This . Txtemail. text;
Break ;

}
}
}
}
}
Try
{
Xmldoc. Save (filename );
Response. Write ("<SCRIPT> alert ('OK, the information has been saved! ') </SCRIPT>");
}
Catch
{
Response. Write ("<SCRIPT> alert ('file writing error. Check whether the Web. config file exists! ') </SCRIPT>");
Return;
}

}

 

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.