当前位置:网站首页>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
边栏推荐
- Jena基于OWL的默认推理查询
- AI视频智能平台EasyCVR设备录像出现无法播放现象的问题修复
- rclone配置minio及基本操作
- Automated test platform (13): interface automation framework and platform comparison, application scenario analysis and design ideas sharing
- The programmer of Beipiao posted a post for help late at night: I am lonely when my girlfriend is gone
- 1286_ Implementation analysis of task priority setting in FreeRTOS
- How to use Alibaba vector font files through CDN
- Ctfhub port scan (SSRF)
- ctfhub-端口扫描(SSRF)
- 手机开户选哪个证券公司比较好,哪个更安全
猜你喜欢
[lingo] find the shortest path problem of undirected graph
Insufficient free space after clearing expired cache entries - consider increasing the maximum cache space
[matlab] solve nonlinear programming
Jena default inference query based on OWL
发现了一个 MySQL 的巨坑:update 更新别再用影响行数做判断了!!!
EasyNVS云管理平台功能重构:支持新增用户、修改信息等
【LINGO】求无向图的最短路问题
[lingo] find the minimum connection diagram of seven cities to minimize the price of natural gas pipelines
Esp32 - ULP coprocessor reading Hall sensor in low power mode
【LINGO】求七个城市最小连线图,使天然气管道价格最低
随机推荐
[recommendation technology] matlab simulation of network information recommendation technology based on collaborative filtering
【推荐系统】美团外卖推荐场景的深度位置交互网络DPIN的突破与畅想
【LINGO】求七个城市最小连线图,使天然气管道价格最低
JSP - paging
Is it safe to buy funds on the brokerage account
LeetCode+ 71 - 75
Chinese explanation of common rclone subcommands
C# Newtonsoft.Json中JObject的使用
【LINGO】求解二次规划
电脑有网络,但所有浏览器网页都打不开,是怎么回事?
Is the account opening of GF Securities safe and reliable? How to open GF Securities Account
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
ctfshow-web355,356(SSRF)
[lingo] find the shortest path problem of undirected graph
[matlab] solve nonlinear programming
rclone 访问web界面
We found a huge hole in MySQL: do not judge the number of rows affected by update!!!
Reply and explanation on issues related to "online training of network security education in 2022"
Jena基于OWL的默认推理查询
iNFTnews | 从《雪崩》到百度“希壤”,元宇宙30年的16件大事