zoukankan      html  css  js  c++  java
  • C# 操作自定义config文件

    示例文件:DB.config

    1.读取

    1  //先实例化一个ExeConfigurationFileMap对象,把物理地址赋值到它的 ExeConfigFilename 属性中;
    2 ExeConfigurationFileMap fileMap = new ExeConfigurationFileMap();
    3 fileMap.ExeConfigFilename = @"DB.config";
    4  //再调用fileMap 实例化 config , 这样,操作的文件就是db.config文件了,也不会产生副本文件
    5 Configuration config = ConfigurationManager.OpenMappedExeConfiguration(fileMap, ConfigurationUserLevel.None);
    6 //获取appsettings节点 
    7 AppSettingsSection appsection = (AppSettingsSection)config.GetSection("appSettings");
    8 string conStrAll = appsection.Settings[ConnectionName].Value;
    View Code


    2.新增

    1 //在appsettings节点中添加元素 
    2 appsection.Settings.Add( "addkey1 ",   "key1 's   value "); 
    3 appsection.Settings.Add( "addkey2 ",   "key2 's   value "); 
    4 config.Save(); 
    View Code


    3.删除

    1 //删除appsettings节点中的元素 
    2 appsection.Settings.Remove( "addkey1 "); 
    3 config.Save(); 
    View Code


    4.修改

    1 //修改appsettings节点中的元素 
    2 appsection.Settings[ "addkey2 "].Value   =   "modify   key2 's   value "; 
    3 config.Save(); 
    View Code
  • 相关阅读:
    Python-02 基础语法
    Python-01 基础语法
    windows端口被占用-解决方案
    vue两个独立的组件之间的传值通信
    URI
    Inversion of control
    7月10日每日总结
    7月9日每日总结
    xshell终端设置主机名和用户名颜色
    PyTorch中一些损失函数的使用
  • 原文地址:https://www.cnblogs.com/yf2011/p/5025654.html
Copyright © 2011-2022 走看看