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、读取connectionStrings

//依据连接串名字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();// ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;也可以直接这样读取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);// ConfigurationManager.ConnectionStrings[connectionName].ConnectionString;也可以直接这样读取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.读取appSettings

///<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();}}//ConfigurationManager.AppSettings[strKey];上边的读取太麻烦  可以直接这样读return null;}

4.修改节点

///<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");}

winform读写config文件相关推荐

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

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

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

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

  3. C# 读写自定义的Config文件

    一.前言 在软件开发中,经常用到设置这样的功能,如果设置中的功能不多,用 Json.XML 这样的数据结构存储非常的麻烦,一个字段的读写,就要写大量的代码,例如 Json 要写实体类才能进行读写,假设 ...

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

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

  5. 奥塔在线:C#创建Winform项目时没有App.config文件的原因

    今天创建了一个测试的Winform项目,在进行数据库链接字符串配置时,发现没有App.config文件,一时有点懵逼. 在网上查了下资料,发现是因为创建项目时采用的.Net框架是4.0版本,而App. ...

  6. 为Config文件提供Application级别的设置读写

    用过设置绑定的应该知道,App.Config和一个文件Settings.Setting是呼应的,开发者可以在Settings.Setting里设置各种设置项 通过设置绑定映射到控件,很多人使用它作为以 ...

  7. WinForm读取指定的config文件的内容

    config文件的使用 一.缘起 最近做项目开始使用C#,因为以前一直使用的是C++,因此面向对象思想方面的知识还是比较全面的,反而是因没有经过完整.系统的.Net方面知识的系统学习,经常被一些在C# ...

  8. java如何读写json文件

    java如何读写json文件 在实际项目开发中,有时会遇到一些全局的配置缓存,最好的做法是配置redis数据库作为数据缓存,而当未有配置redis服务器时,读取静态资源文件(如xml.json等)也是 ...

  9. 读取并修改App.config文件(转载)

    1. 向项目添加app.config文件: 右击项目名称,选择"添加"→"添加新建项",在出现的"添加新项"对话框中,选择"添加应 ...

  10. 在UnitTest中读取*.config文件的郁闷

    - -Visual Stuido 2005集成了Unit Test功能后,创建和管理Unit Test变得比原来简单多了,并且独立项目的Unit Test模块和代码不会再"残留"于 ...

最新文章

  1. R语言构建xgboost模型:交叉验证(cross validation)训练xgboost模型
  2. VTK:通过法线显示颜色用法实战
  3. sublime运行前自动保存代码(转)
  4. php 后端 轻量 框架,GitHub - 22cloud/mixphp: 轻量 PHP 框架,基于 Swoole 的常驻内存型 PHP 高性能框架 (开发文档完善)...
  5. input和textarea的区别
  6. CozyRSS2开发记录0-win10开坑
  7. 基于Spark的电影推荐系统(电影网站)
  8. android适配规则(一)
  9. BZOj 4540: [Hnoi2016]序列 [莫队 st表 预处理]
  10. 使用Ligolo-ng建立隐蔽的通信信道
  11. 交换机VLAN工作模式介绍
  12. http post java工具类_java实现Http post(参数json格式)、get 请求的HttpUtil工具类
  13. Mysql读写分离的原理及配置--amoeba
  14. 联通4g满格但是网速慢_联通4g网络慢是什么原因 联通4g满格但是网速慢
  15. 编解码学习笔记(七):微软Windows Media系列
  16. SSH上传提示:encountered 1 errors during the transfer错误解决办法
  17. Excl2016密码忘记 破解办法
  18. 利用C#生成不重复的随机偶数
  19. 苹果刷机未知错误75_刷机ROOT后遇到Magisk提示需要修复允许环境怎么办?
  20. 拼题A基础篇32 计算圆周率

热门文章

  1. Android SD卡操作
  2. LSTM VS RNN改进
  3. pandas读取与存储操作详解
  4. 服务器显示AL024是什么意思,云端时代云终端快速部署指南(S11AL).ppt
  5. 计算机硬件人员专业知识技能,电子计算机(微机)装配调试员
  6. centos 的命令背景能改吗_精装改毛坯,“降价”促销?“以价换量”还能走得通吗?...
  7. python 3d大数据可视化软件_4个最受欢迎的大数据可视化工具
  8. IIS6 下的Asp.net服务器安全配置
  9. 在Spring Security框架下JWT的实现细节原理
  10. MySQL 索引的原理与应用:索引类型,存储结构与锁