Winform—C#读写config配置文件
现在FrameWork2.0以上使用的是:ConfigurationManager或WebConfigurationManager。并且AppSettings属性是只读的,并不支持修改属性值.
一、如何使用ConfigurationManager?
1、添加引用:添加System.configguration
2、引用空间
3、config配置文件配置节
常用配置节:
(1)普通配置节
<appSettings>
<add key="COM1" value="COM1,9600,8,None,1,已启用" />
</appSettings>
(2)数据源配置节
<connectionStrings>
<add name="kyd" connectionString="server=.;database=UFDATA_999_2017;user=sa;pwd=123"/>
</connectionStrings>
(3)自定义配置节
二、config文件读写
1、依据连接串名字connectionName返回数据连接字符串
//依据连接串名字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;}
2、更新连接字符串
///<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");}
3、返回*.exe.config文件中appSettings配置节的value项
///<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;}
4、在*.exe.config文件中appSettings配置节增加一对键值对
///<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");}
5、修改IP地址
config 读写方法
using System.Configuration; //省略其他代码 public SalesOrderData(){string str = "";str = ConfigurationManager.ConnectionStrings["kyd"].ToString();conn = new SqlConnection(str);cmd = conn.CreateCommand();}
实际应用:
1、获取配置节的值
button1 点击获取配置节<appSettings>指定key的value值
button2 点击获取配置节<connectionStrings>指定name的connectionString值
结果为:
2、修改配置节的值
button1 点击获取配置节<appSettings>指定key的value值
button2 点击修改配置节<connectionStrings>指定key的value值为文本框的值
button3 点击获取配置节<appSettings>指定key新的value值
结果为:
此时配置文件key1的value值为,获取key值仍为修改前的值
如何重置为修改前的值?
如何保存修改后的值?
转载于:https://www.cnblogs.com/xuan666/p/10577861.html
Winform—C#读写config配置文件相关推荐
- WPF读写config配置文件
WPF读写config配置文件单. 1. 在你的工程中,添加app.config文件.文件的内容默认为: 1 <?xml version="1.0" encoding=&qu ...
- C#读写config配置文件
应用程序配置文件(App.config)是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序. 对于一个config ...
- python读取配置文件 分段_Python3读写ini配置文件的示例
ini文件即Initialization File初始化文件,在应用程序及框架中常作为配置文件使用,是一种静态纯文本文件,使用记事本即可编辑. 配置文件的主要功能就是存储一批变量和变量值,在ini文件 ...
- Winform中自定义xml配置文件后对节点进行读取与写入
场景 Winform中自定义xml配置文件,并配置获取文件路径: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100522648 ...
- 在.net中读写config文件的各种方法(转载)
阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CDATA config文件 - Collec ...
- 在.net中读写config文件的各种方法(转)
在.net中读写config文件的各种方法阅读目录 开始 config文件 - 自定义配置节点 config文件 - Property config文件 - Element config文件 - CD ...
- 【机房重构】关于App.config配置文件
App.Config详解 应用程序配置文件是标准的 XML 文件,XML 标记和属性是区分大小写的.它是可以按需要更改的,开发人员可以使用配置文件来更改设置,而不必重编译应用程序. 配置文件的根节点是 ...
- 利用 yaml-cpp 开源库读写 yaml 配置文件—— 读写部分
利用 yaml-cpp 开源库读写 yaml 配置文件-- 读写部分 YAML文件解析 Node 读写yaml文件 config.yaml内容 test_yaml.cpp文件内容 重要内容简单解释 n ...
- C++读写yaml配置文件
文章目录 YAML基础语法 基本规则 三种数据结构 map,散列表 list,数组 scalar,纯量 数据结构嵌套 map嵌套map map嵌套list list嵌套list list嵌套map 利 ...
最新文章
- 你想要什么样的财富自由
- rhel6编译ssh的src.rpm包并修改spec参数
- 网络慢是带宽不足?—Vecloud微云
- pve 群晖 virtio速度慢_群晖DS Note高级教程——教你使用智能记事本
- 1032 挖掘机技术哪家强 (20 分)(c语言)
- 组态王怎么做超级曲线_鸭肉怎么做?大叔教你红烧鸭块,香气扑鼻,简单易做,超级好吃...
- icd植入是大手术吗_母狗绝育是大手术吗?手术完需要住院吗?绝育后是不是会变胖?...
- CodeSmith模板(生成实体类)
- matlab2016一条命令设置坐标轴标签旋转角度、字体大小和字体!
- 17. Window confirm() 方法
- Android调用默认浏览器打开指定url
- windows下的Zcash钱包(ZEC钱包)-zcash4win 1.0.12
- windows命令查找程序_如何在Windows 10上查找和设置屏幕保护程序
- 双硬盘双系统解决引导在同一个分区的问题
- 如何阻止手机虚拟键盘弹起
- 基于JAVA图书商城系统 (Springboot框架) 开题报告
- 论文阅读三:GHM《Gradient Harmonized Single-stage Detector》
- 12306抢票系统——ER图及数据表
- ORA-00001: 违反唯一约束条件 的解决办法
- 优质的MES管理系统,需具备这四个特性
热门文章
- ORACLE startup报错之ORA-01154ORA-01155ORA-01033ORA-03113
- Java 数据类型和 MySql 数据类型对应一览表
- python数据结构list的extend与append的差别
- ROS学习笔记1(配置ROS环境和创建工作空间)
- java判断线段是否相交函数_计算几何-判断线段是否相交
- string字符串数字自增_常见的字符串操作
- 无法访问windows安装服务_win7系统提示无法访问windows安装服务如何解决【解决方法】...
- Graph Embedding图嵌入
- C#关于WebService中Lock锁是否起作用的测试积累
- 美元指数DXY短线走高10逾点,现报90.77