当前位置:网站首页>. Net program configuration file operation (INI, CFG, config)
. Net program configuration file operation (INI, CFG, config)
2022-07-03 06:08:00 【xuhss_ com】
High quality resource sharing
| Learning route guidance ( Click unlock ) | Knowledge orientation | Crowd positioning |
|---|---|---|
| 🧡 Python Actual wechat ordering applet 🧡 | Progressive class | This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system . |
| Python Quantitative trading practice | beginner | Take you hand in hand to create an easy to expand 、 More secure 、 More efficient quantitative trading system |
In the process of program development , We usually use the configuration file to set some parameters . The common configuration file format is ini, xml, config etc. .
INI
.ini file , Usually the initialization file , Is a text file used to store program configuration information .
[Login]
# Turn on encryption 0: Don't open 、1: Turn on
open\_ssl\_certificate=0
.NET The framework itself does not support INI file , You can use Windows API Method uses the platform call service to write and read files .
// The name of the part to be written - sectionName
// The key name to set - key
// The value to set - value
// INI file location - filepath
// Is the read successful - result
[DllImport("kernel32")]
bool WritePrivateProfileString(string sectionName,string key,string value,string filepath);
// The name of the part to read - sectionName
// Key name to read - key
// If the key does not exist, the default value returned - default
// Receive a string used as a buffer - ReturnedVal
// The value actually read - maxsize
// INI file location - filepath
[DllImport("kernel32")]
int GetPrivateProfileString(string sectionName,string key,string default,StringBuilder ReturnedVal,int maxsize,string filepath);
It usually encapsulates a class to call the API Method .
public class ReadWriteINIFile{
...
public void WriteINI(string name, string key, string value)
{
WritePrivateProfileString(name, key, value, _path);
}
public string ReadINI(string name, string key)
{
StringBuilder sb = new StringBuilder(255);
int ini = GetPrivateProfileString(name, key, "", sb, 255, _path);
return sb.ToString();
}
}
CFG
SharpConfig yes .NET Of CFG/INI Profile action component , Read in text or binary format , Modify and save configuration files and streams .
Configuration config = Configuration.LoadFromFile("login.cfg");
Section section = config["Login"];
// Read parameters
bool isOpen = section["open\_ssl\_certificate"].GetValue<bool>();
// Modify the parameters
section["open\_ssl\_certificate"].Value = false;
Config
stay App.config/web.config In the document configSections Under node configuration section node ,.NET Provide its own type for encapsulation .
configSections The node must be configuration Next first node .
NameValue Key value pair
xml version="1.0" encoding="utf-8" ?
<configuration>
<configSections>
<section name="NameValueConfigNode" type="System.Configuration.NameValueSectionHandler"/>
configSections>
<NameValueConfigNode>
<add key="Name One " value="Value One " />
<add key="Name Two " value="Value Two " />
NameValueConfigNode>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
startup>
configuration>
Define a method to get a static property Dictionary Formatted data :
///
/// NameValueCollection
///
public static Dictionary<string, string> NameValueConfigNode
{
get
{
NameValueCollection nvc = (NameValueCollection)ConfigurationManager.GetSection("NameValueConfigNode");
Dictionary<string, string> result = new Dictionary<string,string>();
foreach (string key in nvc.AllKeys)
{
result.Add(key, nvc[key]);
}
return result;
}
}
Dictionary
xml version="1.0" encoding="utf-8" ?
<configuration>
<configSections>
<section name="DictionaryConfigNode" type="System.Configuration.DictionarySectionHandler"/>
configSections>
<DictionaryConfigNode>
<add key="Key One " value="DictValue One " />
<add key="Key Two " value="DictValue Two " />
DictionaryConfigNode>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
startup>
configuration>
///
/// Dictionary
///
public static Dictionary<string, string> DictionaryConfigNode
{
get
{
IDictionary dict = (IDictionary)ConfigurationManager.GetSection("DictionaryConfigNode");
Dictionary<string, string> result = new Dictionary<string, string>();
foreach (string key in dict.Keys)
{
result.Add(key, dict[key].ToString());
}
return result;
}
}
SingTag
xml version="1.0" encoding="utf-8" ?
<configuration>
<configSections>
<section name="SingleTagConfigNode" type="System.Configuration.SingleTagSectionHandler" />
configSections>
<SingleTagConfigNode PropertyOne="1" PropertyTwo="2" PropertyThree="3" PropertyFour="4" PropertyFive="5" />
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
startup>
configuration>
///
/// SingleTag
///
public static Dictionary<string, string> SingleTagConfigNode
{
get
{
Hashtable dict = (Hashtable)ConfigurationManager.GetSection("SingleTagConfigNode");
Dictionary<string, string> result = new Dictionary<string, string>();
foreach (string key in dict.Keys)
{
result.Add(key, dict[key].ToString());
}
return result;
}
}
Custom configuration files
If there are many configuration files , Configuration files can be defined separately , And then in App.config/Web.config Declaration in the document .
xml version="1.0" encoding="utf-8" ?
<configuration>
<configSections>
<section name="MyConfigData1" type="ConsoleApplication.ConfigFiles.ConfigFile,ConsoleApplication"/>
configSections>
<MyConfigData configSource="ConfigFiles\MyConfigFile.config"/>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6" />
startup>
configuration>
Customization files MyConfigFile.config Content :
xml version="1.0" encoding="utf-8" ?
<MyConfigData>
<add key="Key One " value=" Customization file 1 " />
<add key="Key Two " value=" Customization file 2 " />
<add key="Key 3、 ... and " value=" Customization file 3 " />
MyConfigData>
XML
XML Files are often used to simplify the storage and sharing of data , It's designed to transmit data , Instead of displaying data . For complex and irregular configuration information, you can also use XML File for storage .
// Read the file
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("myfile.xml");
// The root node
var nodeRoot = xmlDoc.DocumentElement;
// Create a new node
XmlElement studentNode = xmlDoc.CreateElement("student");
// Create a child node of the new node
XmlElement nameNode = xmlDoc.CreateElement("name");
// Parenting
studentNode.AppendChild(nameNode);
nodeRoot.AppendChild(studentNode);
XML Basic course :https://www.w3school.com.cn/xml/index.asp
My public number

边栏推荐
- [teacher Zhao Yuqiang] Flink's dataset operator
- Simple solution of small up main lottery in station B
- BeanDefinitionRegistryPostProcessor
- Alibaba cloud OOS file upload
- Kubesphere - Multi tenant management
- Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
- Clickhouse learning notes (I): Clickhouse installation, data type, table engine, SQL operation
- Oauth2.0 - use database to store client information and authorization code
- It is said that the operation and maintenance of shell scripts are paid tens of thousands of yuan a month!!!
- [teacher Zhao Yuqiang] the most detailed introduction to PostgreSQL architecture in history
猜你喜欢

Kubesphere - build MySQL master-slave replication structure

Understand the first prediction stage of yolov1

多线程与高并发(7)——从ReentrantLock到AQS源码(两万字大章,一篇理解AQS)

Cesium 点击获三维坐标(经纬度高程)

SVN分支管理

Project summary --04

Oauth2.0 - use database to store client information and authorization code

理解 YOLOV1 第一篇 预测阶段

Strategy pattern: encapsulate changes and respond flexibly to changes in requirements

Kubernetes notes (VI) kubernetes storage
随机推荐
[teacher Zhao Yuqiang] redis's slow query log
BeanDefinitionRegistryPostProcessor
【系统设计】邻近服务
Solve the 1251 client does not support authentication protocol error of Navicat for MySQL connection MySQL 8.0.11
Kubernetes notes (VIII) kubernetes security
Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
Maximum likelihood estimation, divergence, cross entropy
Complete set of C language file operation functions (super detailed)
JS implements the problem of closing the current child window and refreshing the parent window
Es remote cluster configuration and cross cluster search
1. Sum of two numbers
项目总结--04
Why should there be a firewall? This time xiaowai has something to say!!!
There is no one of the necessary magic skills PXE for old drivers to install!!!
Core principles and source code analysis of disruptor
Jedis source code analysis (II): jediscluster module source code analysis
Deep learning, thinking from one dimensional input to multi-dimensional feature input
Multithreading and high concurrency (7) -- from reentrantlock to AQS source code (20000 words, one understanding AQS)
卷积神经网络CNN中的卷积操作详解
Apt update and apt upgrade commands - what is the difference?