应用程序配置文件(App.config)是标准的 XML 文件,XML 标记和属性是区分大小写的。它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序。

对于一个config文件:

<?xml version="1.0" encoding="utf-8" ?>
<configuration> <appSettings> <add key="ServerIP" value="127.0.0.1"></add> <add key="DataBase" value="WarehouseDB"></add> <add key="user" value="sa"></add> <add key="password" value="sa"></add> </appSettings> </configuration>

对config配置文件的读写类:

using System;
using System.Collections.Generic;
using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Configuration; using System.ServiceModel; using System.ServiceModel.Configuration; namespace NetUtilityLib { public static class ConfigHelper { //依据连接串名字connectionName返回数据连接字符串 public static string GetConnectionStringsConfig(string connectionName) { //指定config文件读取 string file = System.Windows.Forms.Application.ExecutablePath; System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(file); string connectionString = config.ConnectionStrings.ConnectionStrings[connectionName].ConnectionString.ToString(); return connectionString; } ///<summary> ///更新连接字符串 ///</summary> ///<param name="newName">连接字符串名称</param> ///<param name="newConString">连接字符串内容</param> ///<param name="newProviderName">数据提供程序名称</param> public static void UpdateConnectionStringsConfig(string newName, string newConString, string newProviderName) { //指定config文件读取 string file = System.Windows.Forms.Application.ExecutablePath; Configuration config = ConfigurationManager.OpenExeConfiguration(file); bool exist = false; //记录该连接串是否已经存在 //如果要更改的连接串已经存在 if (config.ConnectionStrings.ConnectionStrings[newName] != null) { exist = true; } // 如果连接串已存在,首先删除它 if (exist) { config.ConnectionStrings.ConnectionStrings.Remove(newName); } //新建一个连接字符串实例 ConnectionStringSettings mySettings = new ConnectionStringSettings(newName, newConString, newProviderName); // 将新的连接串添加到配置文件中.  config.ConnectionStrings.ConnectionStrings.Add(mySettings); // 保存对配置文件所作的更改  config.Save(ConfigurationSaveMode.Modified); // 强制重新载入配置文件的ConnectionStrings配置节 ConfigurationManager.RefreshSection("ConnectionStrings"); } ///<summary> ///返回*.exe.config文件中appSettings配置节的value项 ///</summary> ///<param name="strKey"></param> ///<returns></returns> public static string GetAppConfig(string strKey) { string file = System.Windows.Forms.Application.ExecutablePath; Configuration config = ConfigurationManager.OpenExeConfiguration(file); foreach (string key in config.AppSettings.Settings.AllKeys) { if (key == strKey) { return config.AppSettings.Settings[strKey].Value.ToString(); } } return null; } ///<summary> ///在*.exe.config文件中appSettings配置节增加一对键值对 ///</summary> ///<param name="newKey"></param> ///<param name="newValue"></param> public static void UpdateAppConfig(string newKey, string newValue) { string file = System.Windows.Forms.Application.ExecutablePath; Configuration config = ConfigurationManager.OpenExeConfiguration(file); bool exist = false; foreach (string key in config.AppSettings.Settings.AllKeys) { if (key == newKey) { exist = true; } } if (exist) { config.AppSettings.Settings.Remove(newKey); } config.AppSettings.Settings.Add(newKey, newValue); config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("appSettings"); } // 修改system.serviceModel下所有服务终结点的IP地址 public static void UpdateServiceModelConfig(string configPath, string serverIP) { Configuration config = ConfigurationManager.OpenExeConfiguration(configPath); ConfigurationSectionGroup sec = config.SectionGroups["system.serviceModel"]; ServiceModelSectionGroup serviceModelSectionGroup = sec as ServiceModelSectionGroup; ClientSection clientSection = serviceModelSectionGroup.Client; foreach (ChannelEndpointElement item in clientSection.Endpoints) { string pattern = @"\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\b"; string address = item.Address.ToString(); string replacement = string.Format("{0}", serverIP); address = Regex.Replace(address, pattern, replacement); item.Address = new Uri(address); } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("system.serviceModel"); } // 修改applicationSettings中App.Properties.Settings中服务的IP地址 public static void UpdateConfig(string configPath, string serverIP) { Configuration config = ConfigurationManager.OpenExeConfiguration(configPath); ConfigurationSectionGroup sec = config.SectionGroups["applicationSettings"]; ConfigurationSection configSection = sec.Sections["DataService.Properties.Settings"]; ClientSettingsSection clientSettingsSection = configSection as ClientSettingsSection; if (clientSettingsSection != null) { SettingElement element1 = clientSettingsSection.Settings.Get("DataService_SystemManagerWS_SystemManagerWS"); if (element1 != null) { clientSettingsSection.Settings.Remove(element1); string oldValue = element1.Value.ValueXml.InnerXml; element1.Value.ValueXml.InnerXml = GetNewIP(oldValue, serverIP); clientSettingsSection.Settings.Add(element1); } SettingElement element2 = clientSettingsSection.Settings.Get("DataService_EquipManagerWS_EquipManagerWS"); if (element2 != null) { clientSettingsSection.Settings.Remove(element2); string oldValue = element2.Value.ValueXml.InnerXml; element2.Value.ValueXml.InnerXml = GetNewIP(oldValue, serverIP); clientSettingsSection.Settings.Add(element2); } } config.Save(ConfigurationSaveMode.Modified); ConfigurationManager.RefreshSection("applicationSettings

转载于:https://www.cnblogs.com/bluedy1229/p/4805558.html

C#读写config配置文件相关推荐

  1. WPF读写config配置文件

    WPF读写config配置文件单. 1. 在你的工程中,添加app.config文件.文件的内容默认为: 1 <?xml version="1.0" encoding=&qu ...

  2. Winform—C#读写config配置文件

    现在FrameWork2.0以上使用的是:ConfigurationManager或WebConfigurationManager.并且AppSettings属性是只读的,并不支持修改属性值. 一.如 ...

  3. python读取配置文件 分段_Python3读写ini配置文件的示例

    ini文件即Initialization File初始化文件,在应用程序及框架中常作为配置文件使用,是一种静态纯文本文件,使用记事本即可编辑. 配置文件的主要功能就是存储一批变量和变量值,在ini文件 ...

  4. 在.net中读写config文件的各种方法(转载)

    阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CDATA config文件 - Collec ...

  5. 在.net中读写config文件的各种方法(转)

    在.net中读写config文件的各种方法阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CD ...

  6. 【机房重构】关于App.config配置文件

    App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序. 配置文件的根节点是 ...

  7. 利用 yaml-cpp 开源库读写 yaml 配置文件—— 读写部分

    利用 yaml-cpp 开源库读写 yaml 配置文件-- 读写部分 YAML文件解析 Node 读写yaml文件 config.yaml内容 test_yaml.cpp文件内容 重要内容简单解释 n ...

  8. C++读写yaml配置文件

    文章目录 YAML基础语法 基本规则 三种数据结构 map,散列表 list,数组 scalar,纯量 数据结构嵌套 map嵌套map map嵌套list list嵌套list list嵌套map 利 ...

  9. Python中logging.config配置文件解读

    Python中logging.config配置文件解读 下面的函数用于配置logging模块,它们位于logging.config模块中.你可以使用这些函数来配置,也可以在logging或是loggi ...

最新文章

  1. [转]建一个XMLHttpRequest对象池
  2. oracle 学习日志
  3. UIControl事件---iOS-Apple苹果官方文档翻译
  4. 元素的层次结构和HTML文档结构
  5. 聊聊高并发(三十五)Java内存模型那些事(三)理解内存屏障
  6. 《机器学习实战》程序清单3-4 创建树的函数代码
  7. 《Beginning Linux Programming》读书笔记(四)
  8. 1年狂赚500亿!中国最土豪的省,究竟是如何称霸全球的?
  9. 科学数字_Excel分列时拒绝让超过15位的数字变成科学计数法
  10. 阿里云php-7.2.12 安装
  11. Glide 4.x之请求网络图片数据流程解析
  12. 今天微软开放了WPF的参考源代码(081026更新)
  13. 思岚激光雷达rplidar从ROS 1到ROS 2的移植
  14. Bought a new glass in BeiJing Pan Jia Yuan
  15. 学会这几个.你就能成为bat脚本小子了...(转来看看的)
  16. Samsung Electronics (三星电子)
  17. SYNS formality 形式验证常见debug 步骤
  18. Firewalld ip伪装和端口转发
  19. 笔记本电脑点开都是计算机,笔记本电脑所有程序都打不开怎么办
  20. 关于华为昆仑关键业务服务器

热门文章

  1. 每个Wi-Fi都有独一无二的IP地址吗?
  2. 如何从0开始,搭建企业的实时数据中台?
  3. 嵌入式软件开发工程师的养成之路——从 推挽输出 开始
  4. sql backup database备份d盘_Oracle-备份与恢复(二)RMAN备份-自动备份计划任务脚本...
  5. delphi 登录界面 主窗体 切换_Python GUI项目实战(二)主窗体的界面设计与实现
  6. python爬取图文新闻_python爬取新闻需要什么软件
  7. pytorch学习笔记(九):softmax回归的简洁实现
  8. Java实验8 T1.编程包含一个标签和一个按钮,在“你好”和“再见”之间切换
  9. 实验7.1 对Point类重载“++”(自增)、“–”(自减)运算符
  10. 卫星协同观测的学习笔记