• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

利用程序动态管理Web.config文件

发布: 2007-6-30 18:56 | 作者: admin | 来源: | 查看: 32次 | 进入软件测试论坛讨论

领测软件测试网  

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;
   }

  }


延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网