当前位置:网站首页>C# 读写自定义的Config文件
C# 读写自定义的Config文件
2022-07-01 07:01:00 【熊思宇】
一、前言
最近搜索了一下自定义Config文件,发现网上大部分帖子都是读写应用程序exe文件对应的.config文件,如下图,读取自定义的配置文件相关帖子很少。
在软件开发中,经常用到设置这样的功能,如果属性不多,用Json、XML 这样的来存储非常的麻烦,一个简单的存储,需要写一大堆东西,有可能还要写实体类才能使用,然后某个属性不用,要删除的地方就有好多个,这时候选择微软自带config文件无疑是最佳选择。
二、添加config文件
可以使用VS自带的添加功能,例如
当然也可以新建一个文本文档,然后改后缀名,再加入内容,都是一样的。
我在软件的根目录里新建了一个Config文件夹,就将配置文件放在这里面了
三、读写配置文件
我们新建一个 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;
}
}
}
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.GetKey("COM1");
//Console.WriteLine(value);
//设置Key
bool result = ConfigHelpers.SetConfigKey("游戏名", "XX信条");
Console.WriteLine("执行完毕");
}
}
}
读取Key
string value = ConfigHelpers.GetKey("COM1");
设置Key
bool result = ConfigHelpers.SetConfigKey("游戏名", "XX信条");
这里我用的是实例化进行读取,如果你有多少个配置文件,就实例化多少个类,然后调用读写,就这么简单。
结束
如果这个帖子对你有用,欢迎 关注 + 点赞 + 留言,谢谢
end
边栏推荐
- [Tikhonov] image super-resolution reconstruction based on Tikhonov regularization
- 运维管理有什么实用的技巧吗
- 图解事件坐标screenX、clientX、pageX, offsetX的区别
- Rclone Chinese document: a collection of common commands
- Postgraduate entrance examination directory link
- go-etcd
- Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
- Docker installation and deployment redis
- Rotate the animation component around the circle, take it and use it directly
- (上)苹果有开源,但又怎样呢?
猜你喜欢
图解事件坐标screenX、clientX、pageX, offsetX的区别
【LINGO】求七个城市最小连线图,使天然气管道价格最低
ESP32 ESP-IDF GPIO按键中断响应
Product learning (I) - structure diagram
ctfshow-web354(SSRF)
Problem: officeexception: failed to start and connect (II)
How to use Alibaba vector font files through CDN
【推荐技术】基于协同过滤的网络信息推荐技术matlab仿真
ctfshow-web352,353(SSRF)
Esp32 monitors the battery voltage with ULP when the battery is powered
随机推荐
C语言实现【扫雷游戏】完整版(实现源码)
[network planning] (I) hub, bridge, switch, router and other concepts
Principle of introducing modules into node
未来互联网人才还稀缺吗?哪些技术方向热门?
[lingo] find the shortest path problem of undirected graph
How to enter the Internet industry and become a product manager? How to become a product manager without project experience?
【计网】(一) 集线器、网桥、交换机、路由器等概念
Solve the problem of "unexpected status code 503 service unavailable" when kaniko pushes the image to harbor
Figure out the difference between event coordinates screenx, clientx, pagex and offsetx
Is it suitable for girls to study product manager? What are the advantages?
Understand esp32 sleep mode and its power consumption
8 figures | analyze Eureka's first synchronization registry
如何进入互联网行业,成为产品经理?没有项目经验如何转行当上产品经理?
【分类模型】Q 型聚类分析
运维面临挑战?智能运维管理系统来帮您
DC-4靶机
Postgraduate entrance examination directory link
【电气介数】电气介数及考虑HVDC和FACTS元件的电气介数计算
脏读、幻读和不可重复读
Database objects: view learning records