当前位置:   article > 正文

C# 读写自定义的Config文件_c#读取config文件

c#读取config文件

一、前言

软件开发中,经常用到设置这样的功能,如果设置中的功能不多,用 Json、XML 这样的数据结构存储非常的麻烦,一个字段的读写,就要写大量的代码,例如 Json 要写实体类才能进行读写,假设其中一个功能不用,这个字段所有相关的引用都要进行删除和修改,使用 ini 这样的方式,现在几乎没几个人用了,于是我决定用微软自带的Config文件方式。

于是搜索了一下自定义Config文件,发现网上大部分帖子都是读写 “软件名.exe.config” 文件,没什么用,和我设计需求不符合,于是我自己写了一个。

二、添加config文件

可以使用VS自带的添加功能,例如

当然也可以新建一个文本文档,然后改后缀名,再加入内容,都是一样的。

我在软件的根目录里新建了一个Config文件夹,就将配置文件放在这里面了

配置文件的名字,这里可以添加多个配置文件

配置文件内容如下:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <configuration>
  3. <appSettings>
  4. <add key="COM1" value="我是一个串口号" />
  5. </appSettings>
  6. </configuration>

三、读写配置文件

我们新建一个 Winform 项目,然后新建一个 ConfigHelper.cs 类,添加 System.Configuration 引用,如果添加不了,就在引用管理器中搜索 System.Configuration,然后就添加到项目中了

  1. using System.Configuration;
  2. internal class ConfigHelper
  3. {
  4. private string ConfigPath = string.Empty;
  5. /// <summary>
  6. /// 获取配置文件指定的Key
  7. /// </summary>
  8. /// <param name="key"></param>
  9. /// <returns></returns>
  10. public string GetConfigKey(string key)
  11. {
  12. Configuration ConfigurationInstance = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
  13. {
  14. ExeConfigFilename = ConfigPath
  15. }, ConfigurationUserLevel.None);
  16. if (ConfigurationInstance.AppSettings.Settings[key] != null)
  17. return ConfigurationInstance.AppSettings.Settings[key].Value;
  18. else
  19. return string.Empty;
  20. }
  21. /// <summary>
  22. /// 设置配置文件指定的Key,如果Key不存在则添加
  23. /// </summary>
  24. /// <param name="key"></param>
  25. /// <param name="vls"></param>
  26. /// <returns></returns>
  27. public bool SetConfigKey(string key, string vls)
  28. {
  29. try
  30. {
  31. Configuration ConfigurationInstance = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
  32. {
  33. ExeConfigFilename = ConfigPath
  34. }, ConfigurationUserLevel.None);
  35. if (ConfigurationInstance.AppSettings.Settings[key] != null)
  36. ConfigurationInstance.AppSettings.Settings[key].Value = vls;
  37. else
  38. ConfigurationInstance.AppSettings.Settings.Add(key, vls);
  39. ConfigurationInstance.Save(ConfigurationSaveMode.Modified);
  40. ConfigurationManager.RefreshSection("appSettings");
  41. return true;
  42. }
  43. catch
  44. {
  45. return false;
  46. }
  47. }
  48. public ConfigHelper(string configPath)
  49. {
  50. ConfigPath = configPath;
  51. }
  52. }

上面的代码可以看到,我将配置文件的路径参数加入到了ConfigHelper的构造函数中去了,这样假设有个多个配置文件,直接实例化就好了。读写互相不相影响。

Form1 界面中我就添加了一个按钮,没有其他的控件,界面就不展示了,代码如下

  1. using System;
  2. using System.Windows.Forms;
  3. using Utils;
  4. namespace Test2
  5. {
  6. public partial class Form1 : Form
  7. {
  8. public Form1()
  9. {
  10. InitializeComponent();
  11. }
  12. private ConfigHelper ConfigHelpers = null;
  13. private void Form1_Load(object sender, EventArgs e)
  14. {
  15. string configPath = Application.StartupPath + "\\Config\\SystemInfo.config";
  16. ConfigHelpers = new ConfigHelper(configPath);
  17. }
  18. private void button1_Click(object sender, EventArgs e)
  19. {
  20. //读取Key
  21. //string value = ConfigHelpers.GetConfigKey("COM1");
  22. //Console.WriteLine(value);
  23. //设置Key
  24. bool result = ConfigHelpers.SetConfigKey("游戏名", "XX信条");
  25. Console.WriteLine("执行完毕");
  26. }
  27. }
  28. }

读取Key

string value = ConfigHelpers.GetConfigKey("COM1");

 设置Key

bool result = ConfigHelpers.SetConfigKey("游戏名", "XX信条");

结束

如果这个帖子对你有用,欢迎 关注 + 点赞 + 留言,谢谢

end

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/2023面试高手/article/detail/83993
推荐阅读
相关标签
  

闽ICP备14008679号