C# 读写自定义的Config文件的实现方法
作者:熊思宇 时间:2022-09-08 23:22:35
一、前言
在软件开发中,经常用到设置这样的功能,如果设置中的功能不多,用 Json、XML 这样的数据结构存储非常的麻烦,一个字段的读写,就要写大量的代码,例如 Json 要写实体类才能进行读写,假设其中一个功能不用,要改的地方还真不少,使用 ini 这样的方式现在几乎没几个人用了,于是我决定用微软自带的Config文件方式。
于是搜索了一下自定义Config文件,发现网上大部分帖子都是读写 “软件名.exe.config” 文件,没什么用,和我设计需求不符合,于是我自己写了一个。
二、添加config文件
可以使用VS自带的添加功能,例如
当然也可以新建一个文本文档,然后改后缀名,再加入内容,都是一样的。
我在软件的根目录里新建了一个Config文件夹,就将配置文件放在这里面了
配置文件的名字,这里可以添加多个配置文件
配置文件内容如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="COM1" value="我是一个串口号" />
</appSettings>
</configuration>
三、读写配置文件
我们新建一个 Winform 项目,然后新建一个 ConfigHelper.cs 类
using System.Configuration;
namespace Utils
{
public class ConfigHelper
{
private string ConfigPath = string.Empty;
/// <summary>
/// 获取配置文件指定的Key
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
public string GetConfigKey(string key)
{
Configuration ConfigurationInstance = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
{
ExeConfigFilename = ConfigPath
}, ConfigurationUserLevel.None);
if (ConfigurationInstance.AppSettings.Settings[key] != null)
return ConfigurationInstance.AppSettings.Settings[key].Value;
else
return string.Empty;
}
/// <summary>
/// 设置配置文件指定的Key,如果Key不存在则添加
/// </summary>
/// <param name="key"></param>
/// <param name="vls"></param>
/// <returns></returns>
public bool SetConfigKey(string key, string vls)
{
try
{
Configuration ConfigurationInstance = ConfigurationManager.OpenMappedExeConfiguration(new ExeConfigurationFileMap()
{
ExeConfigFilename = ConfigPath
}, ConfigurationUserLevel.None);
if (ConfigurationInstance.AppSettings.Settings[key] != null)
ConfigurationInstance.AppSettings.Settings[key].Value = vls;
else
ConfigurationInstance.AppSettings.Settings.Add(key, vls);
ConfigurationInstance.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
return true;
}
catch
{
return false;
}
}
public ConfigHelper(string configPath)
{
ConfigPath = configPath;
}
}
}
上面的代码可以看到,我将配置文件的路径参数加入到了ConfigHelper的构造函数中去了,这样假设有个多个配置文件,直接实例化就好了。读写互相不相影响。
Form1 界面中我就添加了一个按钮,没有其他的控件,界面就不展示了,代码如下
using System;
using System.Windows.Forms;
using Utils;
namespace Test2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private ConfigHelper ConfigHelpers = null;
private void Form1_Load(object sender, EventArgs e)
{
string configPath = Application.StartupPath + "\\Config\\SystemInfo.config";
ConfigHelpers = new ConfigHelper(configPath);
}
private void button1_Click(object sender, EventArgs e)
{
//读取Key
//string value = ConfigHelpers.GetConfigKey("COM1");
//Console.WriteLine(value);
//设置Key
bool result = ConfigHelpers.SetConfigKey("游戏名", "XX信条");
Console.WriteLine("执行完毕");
}
}
}
读取Key
string value = ConfigHelpers.GetConfigKey("COM1");
设置Key
bool result = ConfigHelpers.SetConfigKey("游戏名", "XX信条");
来源:https://blog.csdn.net/qq_38693757/article/details/125549559
标签:C#,读写,Config
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
c#的dllimport使用方法详解
2023-04-20 04:01:49
mybatis-spring:@MapperScan注解的使用
2021-11-22 09:58:33
![](https://img.aspxhome.com/file/2023/0/75410_0s.png)
C#实现简易多人聊天室
2023-02-06 06:09:47
![](https://img.aspxhome.com/file/2023/6/122616_0s.jpg)
springBoot解决static和@Component遇到的bug
2022-12-31 01:23:27
C# Split分隔字符串的应用(C#、split、分隔、字符串)
2021-12-01 23:13:51
MyBatis-Plus 自定义sql语句的实现
2022-12-24 23:16:16
Android音视频开发之VideoView使用指南
2022-11-20 11:14:52
![](https://img.aspxhome.com/file/2023/5/137975_0s.png)
C#中IEnumerable、ICollection、IList、List之间的区别
2022-07-27 18:15:07
c# HttpClient设置超时的步骤
2023-05-07 10:42:29
Android测量每秒帧数Frames Per Second (FPS)的方法
2022-01-21 05:43:15
java中Hibernate缓存形式总结
2023-10-19 15:57:43
Java两个乒乓球队比赛名单问题(判断素数)
2022-11-15 08:39:20
springboot+thymeleaf+druid+mybatis 多模块实现用户登录功能
2022-09-17 21:36:41
![](https://img.aspxhome.com/file/2023/8/119178_0s.jpg)
浅谈SpringBoot @Autowired的两种注入方式
2021-06-28 06:08:34
Java深度复制功能与用法实例分析
2023-07-05 11:23:17
详解SpringBoot缓存的实例代码(EhCache 2.x 篇)
2023-04-05 04:55:18
![](https://img.aspxhome.com/file/2023/1/77931_0s.jpg)
解决springboot遇到autowire注入为null的问题
2022-04-15 02:37:59
![](https://img.aspxhome.com/file/2023/4/89624_0s.jpg)
C#探秘系列(一)——ToDictionary,ToLookup
2023-04-19 09:16:15
![](https://img.aspxhome.com/file/2023/2/100092_0s.png)
Android AIDL通信DeadObjectException解决方法示例
2022-06-04 14:53:27
Spring Boot整合Redis的完整步骤
2023-06-03 03:21:56
![](https://img.aspxhome.com/file/2023/2/67652_0s.png)