zoukankan      html  css  js  c++  java
  • Configuration类在网页实现对web.config的修改[转]

    【IT168技术文档】 

      一、ConfigurationManager.OpenExeConfiguration的问题 
      ConfigurationManager.OpenExeConfiguration有两个重载, 
      ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal)和ConfigurationManager.OpenExeConfiguration(Server.MapPath("web.config")); 

      经过实验我发现,在网上上,ConfigurationManager.OpenExeConfiguration (ConfigurationUserLevel.PerUserRoamingAndLocal)不能使用,而Configuration和 ConfigurationManager.OpenExeConfiguration其实不能直接对文件进行修改。

         Configuration cfg = ConfigurationManager.OpenExeConfiguration(Server.MapPath("web.config"));
    Response.Write(cfg.FilePath);
    

      你会发现以上代码输出的FilePath是F:路径Webweb.config.config 。打开的文件虽然看的是web.config,其实是另外一个文件web.config.config,虽然web.config.config并不存在。 

      而进行以下操作

     Configuration cfg = ConfigurationManager.OpenExeConfiguration(Server.MapPath("web.config"));
    cfg.AppSettings.Settings.Remove("123");
    cfg.AppSettings.Settings.Add("123","asdasd");
    cfg.Save();
    

      读不出web.config的内容,而实际上是读的web.config.config的内容。我们现在没这个文件,那么内容就是空的。保存之后,得到

    <?xml version="1.0" encoding="utf-8"?><configuration><appSettings><add key="123" value="asdasd"/></appSettings></configuration>
    

      二、引出来的思路 
      可以建立个文件web.config.config,作为web.config的副本。 
      再次执行

            Configuration cfg = ConfigurationManager.OpenExeConfiguration(Server.MapPath("web.config"));
    cfg.AppSettings.Settings.Remove("123");
    cfg.AppSettings.Settings.Add("123","asdasd");
    Response.Write(cfg.FilePath);
    cfg.SaveAs(Server.MapPath("web.config"));
    

      操作,则确实实现了对文件的修改。

    【原地址】:http://tech.sina.com.cn/s/2008-06-30/1013712947.shtml

  • 相关阅读:
    市面上的系统.
    linux /dev 常见特殊设备介绍与应用[loop,null,zero,full,random]
    Linux command 系统快捷键
    Qtcreator中常用快捷键总结
    开始新的博客征途
    css选择器顺序的小技巧
    推荐20个很有帮助的web前端开发教程
    炙手可热的前端资源大集合
    一款纯css实现的垂直时间线效果
    15款极具创造性的艺术创意
  • 原文地址:https://www.cnblogs.com/yongtaiyu/p/3594182.html
Copyright © 2011-2022 走看看