当前位置:网站首页>C # read and write customized config file
C # read and write customized config file
2022-07-01 07:09:00 【Xiongsiyu】
One 、 Preface
Recently, I searched for customization Config file , I found that most posts on the Internet are read-write applications exe File corresponding .config file , Here's the picture , There are few posts about reading customized configuration files .

In software development , The function of setting is often used , If there are not many attributes , use Json、XML Such storage is very troublesome , A simple storage , I need to write a lot of things , You may have to write entity classes to use , Then a certain attribute is not used , There are many places to delete , At this time, choose Microsoft to bring config File is undoubtedly the best choice .
Two 、 add to config file
have access to VS Built in add-on function , for example

Of course, you can also create a new text document , Then change the suffix , Add content again , It's all the same .
I created a new one in the root directory of the software Config Folder , Put the configuration file here


3、 ... and 、 Read and write configuration files
We build a new one Winform project , And then create a new one ConfigHelper.cs class
using System.Configuration;
namespace Utils
{
public class ConfigHelper
{
private string ConfigPath = string.Empty;
/// <summary>
/// Get the 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>
/// Set the... Specified in the configuration file Key, If Key Add if not
/// </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 I added a button in , There are no other controls , The code is as follows
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)
{
// Read Key
//string value = ConfigHelpers.GetKey("COM1");
//Console.WriteLine(value);
// Set up Key
bool result = ConfigHelpers.SetConfigKey(" Game name ", "XX Creed ");
Console.WriteLine(" completion of enforcement ");
}
}
}
Read Key
string value = ConfigHelpers.GetKey("COM1");Set up Key
bool result = ConfigHelpers.SetConfigKey(" Game name ", "XX Creed ");Here I use instantiation to read , If you have how many profiles , Just instantiate how many classes , Then call read / write , It's that simple .
end
If this post is useful to you , welcome Focus on + give the thumbs-up + Leaving a message. , thank you
end
边栏推荐
- 【Tikhonov】基于Tikhonov正则化的图像超分辨率重建
- ctfshow-web355,356(SSRF)
- Servlet 和 JSP 中的分页
- buildroot override 机制
- Summary of the concept and advantages of 5g massive MIMO
- WiFi settings for raspberry Pie 4
- 如何进入互联网行业,成为产品经理?没有项目经验如何转行当上产品经理?
- Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
- ctfshow-web354(SSRF)
- 8 figures | analyze Eureka's first synchronization registry
猜你喜欢

Are there any practical skills for operation and maintenance management

Ctfhub port scan (SSRF)

如何制作专属的VS Code主题
![[FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera](/img/0f/045957961725716435439316078347.png)
[FPGA frame difference] FPGA implementation of frame difference target tracking based on vmodcam camera

Insufficient free space after clearing expired cache entries - consider increasing the maximum cache space

发现了一个 MySQL 的巨坑:update 更新别再用影响行数做判断了!!!

Will Internet talents be scarce in the future? Which technology directions are popular?

Spark入门(一篇就够了)

继妹变继母,儿子与自己断绝关系,世界首富马斯克,为何这么惨?

Inventory the six second level capabilities of Huawei cloud gaussdb (for redis)
随机推荐
Operation and maintenance management system, humanized operation experience
Unity2021-Scene视图中物体无法直接选中的解决办法
Jena基于OWL的默认推理查询
[lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines
开源了!文心大模型ERNIE-Tiny轻量化技术,又准又快,效果全开
C# Newtonsoft.Json中JObject的使用
Product learning (III) - demand list
Router 6/ and the difference with router5
Kdtree notes
為什麼這麼多人轉行產品經理?產品經理發展前景如何?
【计网】(一) 集线器、网桥、交换机、路由器等概念
Is fixed investment fund a high-risk product?
Is it reliable to open an account on the compass with your mobile phone? Is there any potential safety hazard
Reply and explanation on issues related to "online training of network security education in 2022"
EasyNVS云管理平台功能重构:支持新增用户、修改信息等
Chinese explanation of common rclone subcommands
Esp32 monitors the battery voltage with ULP when the battery is powered
微软宣布开源 (GODEL) 语言模型聊天机器人
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
Is it safe to buy funds on the brokerage account