zoukankan      html  css  js  c++  java
  • WebConfigurationManager读写配置文件 枫

    .net1.1中如果需要灵活的操作和读写配置文件并不是十分方便,一般都会在项目中封装一个配置文件管理类来进行读写操作。而在.net2.0中使用ConfigurationManager 和WebConfigurationManager 类可以很好的管理配置文件,ConfigurationManager类在System.Configuration 中,WebConfigurationManager在System.Web.Configuration中。根据MSDN的解释,对于 Web 应用程序配置,建议使用System.Web.Configuration.WebConfigurationManager 类,而不要使用 System.Configuration.ConfigurationManager 类。

    如何使用WebConfigurationManager操作配置文件:

    WebConfigurationManager读写配置文件       //打开配置文件
    WebConfigurationManager读写配置文件
             Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    WebConfigurationManager读写配置文件        
    //获取appSettings节点
    WebConfigurationManager读写配置文件
             AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
    WebConfigurationManager读写配置文件        
    //在appSettings节点中添加元素
    WebConfigurationManager读写配置文件
             appSection.Settings.Add("addkey1""key1's value");
    WebConfigurationManager读写配置文件         appSection.Settings.Add(
    "addkey2""key2's value");
    WebConfigurationManager读写配置文件         config.Save();


    运行代码之后可以看见配置文件中的改变:

    WebConfigurationManager读写配置文件<appSettings>
    WebConfigurationManager读写配置文件  
    <add key="addkey1" value="key1's value" />
    WebConfigurationManager读写配置文件  
    <add key="addkey2" value="key2's value" />
    WebConfigurationManager读写配置文件
    </appSettings>

    修改和删除节点或属性也非常方便:

    WebConfigurationManager读写配置文件       //打开配置文件
    WebConfigurationManager读写配置文件
             Configuration config = WebConfigurationManager.OpenWebConfiguration("~");
    WebConfigurationManager读写配置文件        
    //获取appSettings节点
    WebConfigurationManager读写配置文件
             AppSettingsSection appSection = (AppSettingsSection)config.GetSection("appSettings");
    WebConfigurationManager读写配置文件        
    //删除appSettings节点中的元素
    WebConfigurationManager读写配置文件
             appSection.Settings.Remove("addkey1");
    WebConfigurationManager读写配置文件        
    //修改appSettings节点中的元素
    WebConfigurationManager读写配置文件
             appSection.Settings["addkey2"].Value = "Modify key2's value";
    WebConfigurationManager读写配置文件         config.Save();

    配置文件:
    WebConfigurationManager读写配置文件<appSettings>
    WebConfigurationManager读写配置文件   
    <add key="addkey2" value="Modify key2's value" />
    WebConfigurationManager读写配置文件 
    </appSettings>
     
    IOS
  • 相关阅读:
    原生和jQuery的ajax用法
    sublime常用快捷键
    用filter:grayscale将图片过滤成灰色
    Docker搭建Zookeeper集群问题总结
    Linux下jdk环境配置
    window MySQL解压缩版部署及配置
    Windows下Nginx的配置及配置文件部分介绍
    JS 特性:可选链(?.)
    509道Java面试题解析:2020年最新Java面试题
    阿里面试题BIO和NIO数量问题附答案和代码
  • 原文地址:https://www.cnblogs.com/mrray/p/2751739.html
Copyright © 2011-2022 走看看