C 读写config配置文件

C 读写config配置文件本文详细介绍了 C 项目中的默认配置文件 App config 编译后生成的 exe config 和 vshost exe config 的区别与操作方法 包括如何使用 System Configura

大家好,欢迎来到IT知识分享网。

一、项目默认配置文件

1、app.config

2、*.exe.config

3、*.vshost.exe.config

二、读写配置文件

1、添加 System.Configuration 程序集文件

2、Config读取操作类函数

using System; using System.Collections.Generic; using System.Configuration; using System.Linq; using System.Text; using System.Threading.Tasks; namespace FileOperationsDemo { 
    public static class ReadWriteConfig { 
    #region 在系统配置文件中 读/写配置相关函数 /// <summary> /// 从 *.exe.config 或 *.vshost.exe.config 文件中读取数据,返回字符串 /// </summary> /// <param name="Setting_name"></param> /// <returns></returns> public static String Read_appSetting(String Setting_name) { 
    String str = ""; //获取Configuration对象 Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //根据Key读取<add>元素的Value str = config.AppSettings.Settings[Setting_name].Value; return str; } /// <summary> /// 从 *.exe.config 或 *.vshost.exe.config 文件中读取数据,处理后返回List列表 /// </summary> /// <param name="Setting_name"></param> /// <returns></returns> public static List<String> Read_appSetting_List(String Setting_name) { 
    String str = ""; //获取Configuration对象 Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); //根据Key读取<add>元素的Value str = config.AppSettings.Settings[Setting_name].Value; string[] tmp = str.Split(','); List<string> ret_list = new List<string>(); ret_list.Clear(); for (Int32 i = 0; i < tmp.Length; i++) { 
    ret_list.Add(tmp[i]); } return ret_list; } /// <summary> /// 将配置写入 *.exe.config 或 *.vshost.exe.config 文件中 /// </summary> /// <param name="Setting_name"></param> /// <param name="value"></param> public static void Write_appSetting(String Setting_name, String value) { 
    //获取Configuration对象 Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.Settings[Setting_name].Value = value; //写入<add>元素的Value //一定要记得保存,写不带参数的config.Save()也可以 config.Save(ConfigurationSaveMode.Modified); 刷新,否则程序读取的还是之前的值(可能已装入内存) System.Configuration.ConfigurationManager.RefreshSection("appSettings"); } /// <summary> /// 将配置写入 *.exe.config 或 *.vshost.exe.config 文件中 /// </summary> /// <param name="name"></param> /// <param name="Setting_name"></param> public static void Write_appSetting_List(String name, List<String> Setting_name) { 
    String str = ""; for (Int32 i = 0; i < Setting_name.Count - 1; i++) { 
    str += Setting_name[i] + ","; } str += Setting_name[Setting_name.Count - 1]; //获取Configuration对象 Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None); config.AppSettings.Settings[name].Value = str; //写入<add>元素的Value //一定要记得保存,写不带参数的config.Save()也可以 config.Save(ConfigurationSaveMode.Modified); 刷新,否则程序读取的还是之前的值(可能已装入内存) System.Configuration.ConfigurationManager.RefreshSection("appSettings"); } #endregion #region appSettings 读/写配置相关函数 根据路径 /// <summary> /// 从 path 指定文件中读取数据,返回字符串 /// </summary> /// <param name="path"></param> /// <param name="Setting_name"></param> /// <returns></returns> public static String Read_appSettingPath(String path, String Setting_name) { 
    String str = ""; ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = @path; //获取Configuration对象 Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); //根据Key读取<add>元素的Value str = config.AppSettings.Settings[Setting_name].Value; return str; } /// <summary> /// 从 path 指定文件中读取数据,返回字符串,处理后返回List列表 /// </summary> /// <param name="path"></param> /// <param name="Setting_name"></param> /// <returns></returns> public static List<String> Read_appSettingPath_List(String path, String Setting_name) { 
    String str = ""; ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = @path; //获取Configuration对象 Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); //根据Key读取<add>元素的Value str = config.AppSettings.Settings[Setting_name].Value; string[] tmp = str.Split(','); List<string> ret_list = new List<string>(); ret_list.Clear(); for (Int32 i = 0; i < tmp.Length; i++) { 
    ret_list.Add(tmp[i]); } return ret_list; } /// <summary> /// 将配置写入path路径的.config文件中 /// </summary> /// <param name="path">路径</param> /// <param name="Setting_name">键</param> /// <param name="value">值</param> public static void Write_appSettingPath(String path, String Setting_name, String value) { 
    ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = @path; //获取Configuration对象 Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); config.AppSettings.Settings[Setting_name].Value = value; //写入<add>元素的Value //一定要记得保存,写不带参数的config.Save()也可以 config.Save(ConfigurationSaveMode.Modified); 刷新,否则程序读取的还是之前的值(可能已装入内存) System.Configuration.ConfigurationManager.RefreshSection("appSettings"); } /// <summary> /// 将配置写入path路径的.config文件中 /// </summary> /// <param name="path">路径</param> /// <param name="name">键</param> /// <param name="Setting_name">值</param> public static void Write_appSettingPath_List(String path, String name, List<String> Setting_name) { 
    String str = ""; for (Int32 i = 0; i < Setting_name.Count - 1; i++) { 
    str += Setting_name[i] + ","; } str += Setting_name[Setting_name.Count - 1]; ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = @path; //获取Configuration对象 Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); config.AppSettings.Settings[name].Value = str; //写入<add>元素的Value //一定要记得保存,写不带参数的config.Save()也可以 config.Save(ConfigurationSaveMode.Modified); 刷新,否则程序读取的还是之前的值(可能已装入内存) System.Configuration.ConfigurationManager.RefreshSection("appSettings"); } ///<summary>  ///在path路径的.config文件中增加一对键值对  ///</summary>  ///<param name="newKey"></param>  ///<param name="newValue"></param>  public static void ADDAppConfig(String path, String newKey, String newValue) { 
    ExeConfigurationFileMap map = new ExeConfigurationFileMap(); map.ExeConfigFilename = @path; //获取Configuration对象 Configuration config = System.Configuration.ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None); 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"); } #endregion } } 

3、使用用例

 /// <summary> /// 读取config文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button5_Click(object sender, EventArgs e) { 
    string serialPortCount = ReadWriteConfig.Read_appSetting("serialPortCount"); richTextBox3.AppendText(serialPortCount + "\t\n"); string Constr = ReadWriteConfig.Read_appSettingPath(Application.StartupPath + "\\config\\InitializeConfig.config", "修正器"); richTextBox3.AppendText(Constr + "\t\n"); } /// <summary> /// 写入config文件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button6_Click(object sender, EventArgs e) { 
    ReadWriteConfig.Write_appSetting("serialPortCount", richTextBox3.Text.ToString().Trim()); string path = Application.StartupPath + "\\config\\InitializeConfig.config"; ReadWriteConfig.Write_appSetting("serialPort1", richTextBox3.Lines[0].ToString().Trim()); ReadWriteConfig.Write_appSettingPath(path, "修正器", richTextBox3.Lines[0].ToString().Trim()); ReadWriteConfig.ADDAppConfig(path, "修正器11", richTextBox3.Lines[1].ToString().Trim()); } 

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/116716.html

(0)
上一篇 2025-11-26 20:20
下一篇 2025-11-26 20:33

相关推荐

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注微信