当前位置:网站首页>. 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

边栏推荐
- Skywalking8.7 source code analysis (I): agent startup process, agent configuration loading process, custom class loader agentclassloader, plug-in definition system, plug-in loading
- Exception when introducing redistemplate: noclassdeffounderror: com/fasterxml/jackson/core/jsonprocessingexception
- Txt document download save as solution
- It is said that the operation and maintenance of shell scripts are paid tens of thousands of yuan a month!!!
- [explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
- Apple submitted the new MAC model to the regulatory database before the spring conference
- Exportation et importation de tables de bibliothèque avec binaires MySQL
- If function of MySQL
- 1. 两数之和
- Svn branch management
猜你喜欢
![[teacher Zhao Yuqiang] index in mongodb (Part 2)](/img/a7/2140744ebad9f1dc0a609254cc618e.jpg)
[teacher Zhao Yuqiang] index in mongodb (Part 2)
![[teacher Zhao Yuqiang] Flink's dataset operator](/img/cc/5509b62756dddc6e5d4facbc6a7c5f.jpg)
[teacher Zhao Yuqiang] Flink's dataset operator

Zhiniu stock -- 03

Life is a process of continuous learning

Jedis source code analysis (II): jediscluster module source code analysis

最大似然估计,散度,交叉熵
![[teacher Zhao Yuqiang] calculate aggregation using MapReduce in mongodb](/img/cc/5509b62756dddc6e5d4facbc6a7c5f.jpg)
[teacher Zhao Yuqiang] calculate aggregation using MapReduce in mongodb

Pytorch builds the simplest version of neural network

GPS坐标转百度地图坐标的方法

Kubernetes notes (I) kubernetes cluster architecture
随机推荐
.NET程序配置文件操作(ini,cfg,config)
Cesium 点击获取模型表面经纬度高程坐标(三维坐标)
Bernoulli distribution, binomial distribution and Poisson distribution, and the relationship between maximum likelihood (incomplete)
Oauth2.0 - use database to store client information and authorization code
[teacher Zhao Yuqiang] kubernetes' probe
Understand one-way hash function
[teacher Zhao Yuqiang] Alibaba cloud big data ACP certified Alibaba big data product system
CAD插件的安装和自动加载dll、arx
Multithreading and high concurrency (7) -- from reentrantlock to AQS source code (20000 words, one understanding AQS)
Leetcode solution - 01 Two Sum
BeanDefinitionRegistryPostProcessor
JDBC connection database steps
[trivia of two-dimensional array application] | [simple version] [detailed steps + code]
Bio, NiO, AIO details
Complete set of C language file operation functions (super detailed)
Clickhouse learning notes (2): execution plan, table creation optimization, syntax optimization rules, query optimization, data consistency
Beandefinitionregistrypostprocessor
The most responsible command line beautification tutorial
[Zhao Yuqiang] deploy kubernetes cluster with binary package
Project summary --04