zoukankan      html  css  js  c++  java
  • 利用程序动态管理Web.config文件

    Web.config文件假设有如下需要管理的配置信息: 

    <appSettings>
        <add key="SiteTitle" value="站点名称" />
        <add key="SiteUrl" value="主页网址" />
        <add key="SiteLogo" value="站点Logo" />
        <add key="SiteBanner" value="站点Banner" />
        <add key="SiteEmail" value="联系Email" />
    </appSettings>

    实现的c#核心代码:

    一、将Web.config中的相关信息读入TextBox

    private void Page_Load(object sender, System.EventArgs e)
      {
       if(!Page.IsPostBack)
       {
        //将Web.config中的相关值填入TextBox
        this.txtTitle.Text=System.Configuration.ConfigurationSettings.AppSettings["SiteTitle"];
        this.txtUrl.Text=System.Configuration.ConfigurationSettings.AppSettings["SiteUrl"];
        this.txtLogo.Text=System.Configuration.ConfigurationSettings.AppSettings["SiteLogo"];
        this.txtBanner.Text=System.Configuration.ConfigurationSettings.AppSettings["SiteBanner"];
        this.txtEmail.Text=System.Configuration.ConfigurationSettings.AppSettings["SiteEmail"];
       }

      }

    二、将修改后的内容写入Web.config

      private void btnSave_Click(object sender, System.EventArgs e)
      {
       string filename=Server.MapPath("web.config");
       string KeyName;//键名称

       XmlDocument  xmldoc= new XmlDocument();
       try
       {
        xmldoc.Load(filename);
       }
       catch
       {
        Response.Write("<script>alert('读文件时错误,请检查 Web.config 文件是否存在!')</script>");
        return;
       }
       
       XmlNodeList DocdNodeNameArr=xmldoc.DocumentElement.ChildNodes;//文档节点名称数组
       foreach(XmlElement DocXmlElement in DocdNodeNameArr)
       {
        if(DocXmlElement.Name.ToLower()=="appsettings")//找到名称为 appsettings 的节点
        {
         XmlNodeList KeyNameArr=DocXmlElement.ChildNodes;//子节点名称数组
         if ( KeyNameArr.Count >0 )
         {
          foreach(XmlElement xmlElement in KeyNameArr)
          {
           KeyName=xmlElement.Attributes["key"].InnerXml;//键值
           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,信息已保存!')</script>");
       }
       catch
       {
        Response.Write("<script>alert('写文件时错误,请检查 Web.config 文件是否存在!')</script>");
        return;
       }

      }

    申明

    非源创博文中的内容均收集自网上,若有侵权之处,请及时联络,我会在第一时间内删除.再次说声抱歉!!!

    博文欢迎转载,但请给出原文连接。

  • 相关阅读:
    燕园笔记之一——穿梭
    openvc学习笔记(1)——简单应用
    匆匆过客
    opencv学习(3)——高斯平滑图像
    为什么(win10系统)电脑上面的鼠标(触摸板)突然用不了?怎么开启和关闭、
    SqlServer自动备份数据库
    CodeSmith批量生成实体类
    XML文件的序列化和反序列化
    AjaxPro的使用
    设计模式之——策略模式
  • 原文地址:https://www.cnblogs.com/Athrun/p/823404.html
Copyright © 2011-2022 走看看