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

推荐订阅源

Blog — PlanetScale
Blog — PlanetScale
SecWiki News
SecWiki News
Google DeepMind News
Google DeepMind News
WordPress大学
WordPress大学
小众软件
小众软件
C
CERT Recently Published Vulnerability Notes
Jina AI
Jina AI
N
Netflix TechBlog - Medium
GbyAI
GbyAI
IT之家
IT之家
Apple Machine Learning Research
Apple Machine Learning Research
AWS News Blog
AWS News Blog
G
GRAHAM CLULEY
L
Lohrmann on Cybersecurity
C
Cybersecurity and Infrastructure Security Agency CISA
I
Intezer
T
Tor Project blog
P
Palo Alto Networks Blog
P
Privacy & Cybersecurity Law Blog
P
Privacy International News Feed
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
P
Proofpoint News Feed
T
Tailwind CSS Blog
C
Check Point Blog
Cloudbric
Cloudbric
Y
Y Combinator Blog
The Last Watchdog
The Last Watchdog
Forbes - Security
Forbes - Security
Last Week in AI
Last Week in AI
S
Security Affairs
博客园 - Franky
F
Fortinet All Blogs
量子位
M
MIT News - Artificial intelligence
C
Cisco Blogs
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
S
Secure Thoughts
V
Visual Studio Blog
AI
AI
美团技术团队
B
Blog RSS Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
博客园 - 三生石上(FineUI控件)
阮一峰的网络日志
阮一峰的网络日志
Engineering at Meta
Engineering at Meta
人人都是产品经理
人人都是产品经理
Microsoft Security Blog
Microsoft Security Blog
T
Threatpost
Cyberwarzone
Cyberwarzone

博客园 - 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;
   }


  }