惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
爱范儿
爱范儿
人人都是产品经理
人人都是产品经理
博客园 - 司徒正美
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
量子位
罗磊的独立博客
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
小众软件
小众软件
C
Cybersecurity and Infrastructure Security Agency CISA
Cyberwarzone
Cyberwarzone
大猫的无限游戏
大猫的无限游戏
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
雷峰网
雷峰网
Simon Willison's Weblog
Simon Willison's Weblog
The Cloudflare Blog
博客园 - 三生石上(FineUI控件)
D
Darknet – Hacking Tools, Hacker News & Cyber Security
C
Cyber Attacks, Cyber Crime and Cyber Security
博客园_首页
博客园 - 叶小钗
V
Vulnerabilities – Threatpost
T
The Exploit Database - CXSecurity.com
T
Tailwind CSS Blog
IT之家
IT之家
博客园 - 聂微东
Spread Privacy
Spread Privacy
V2EX - 技术
V2EX - 技术
S
Security Affairs
宝玉的分享
宝玉的分享
V
V2EX
C
Cisco Blogs
博客园 - Franky
美团技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
S
Securelist
J
Java Code Geeks
Webroot Blog
Webroot Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Proofpoint News Feed
Last Week in AI
Last Week in AI
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
WordPress大学
WordPress大学
W
WeLiveSecurity
T
Threatpost
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志

博客园 - Ansel

如何做到某个网站被访问? 也谈项目经理 项目团队几个发展阶段总结 如何当好一个项目经理(引用) 微创面试归来话感想 .NET框架垃圾回收机制 二分法——查找、排序以及库函数bsearch的用法 关于web.config - Ansel - 博客园 使用C#和MSMQ开发消息处理程序 访问数据库时如何解决并发问题 very nice to have talked with you via phone 压缩ASP.NET中的ViewState 使用ASP.NET Global.asax 文件--基础篇 ASP.NET 2.0 的内部变化 .Net调用Java的WebService之亲身体验 服务器端异步 Web 方法 在 ASP.NET 中支持数据库缓存相关性 关于 .NET Passport 身份验证 用.Net开发Windows服务初探
利用程序动态管理Web.config文件
Ansel · 2005-08-31 · via 博客园 - Ansel

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


  }