赞
踩
在软件开发中,经常用到设置这样的功能,如果设置中的功能不多,用 Json、XML 这样的数据结构存储非常的麻烦,一个字段的读写,就要写大量的代码,例如 Json 要写实体类才能进行读写,假设其中一个功能不用,这个字段所有相关的引用都要进行删除和修改,使用 ini 这样的方式,现在几乎没几个人用了,于是我决定用微软自带的Config文件方式。
于是搜索了一下自定义Config文件,发现网上大部分帖子都是读写 “软件名.exe.config” 文件,没什么用,和我设计需求不符合,于是我自己写了一个。
可以使用VS自带的添加功能,例如
当然也可以新建一个文本文档,然后改后缀名,再加入内容,都是一样的。
我在软件的根目录里新建了一个Config文件夹,就将配置文件放在这里面了
配置文件的名字,这里可以添加多个配置文件
配置文件内容如下:
- <?xml version="1.0" encoding="utf-8" ?>
- <configuration>
- <appSettings>
- <add key="COM1" value="我是一个串口号" />
- </appSettings>
- </configuration>
我们新建一个 Winform 项目,然后新建一个 ConfigHelper.cs 类,添加 System.Configuration 引用,如果添加不了,就在引用管理器中搜索 System.Configuration,然后就添加到项目中了
- using System.Configuration;
-
- internal 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信条");
如果这个帖子对你有用,欢迎 关注 + 点赞 + 留言,谢谢
end
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。