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

边栏推荐
- Kubernetes notes (I) kubernetes cluster architecture
- If function of MySQL
- When PHP uses env to obtain file parameters, it gets strings
- BeanDefinitionRegistryPostProcessor
- [teacher Zhao Yuqiang] MySQL high availability architecture: MHA
- PMP笔记记录
- Clickhouse learning notes (I): Clickhouse installation, data type, table engine, SQL operation
- Common exceptions when Jenkins is released (continuous update...)
- PHP用ENV获取文件参数的时候拿到的是字符串
- Installation of CAD plug-ins and automatic loading of DLL and ARX
猜你喜欢

Oauth2.0 - using JWT to replace token and JWT content enhancement

Exception when introducing redistemplate: noclassdeffounderror: com/fasterxml/jackson/core/jsonprocessingexception

Oauth2.0 - user defined mode authorization - SMS verification code login

【系统设计】邻近服务

QT read write excel -- qxlsx insert chart 5
![[trivia of two-dimensional array application] | [simple version] [detailed steps + code]](/img/84/98c1220d0f7bc3a948125ead6ff3d9.jpg)
[trivia of two-dimensional array application] | [simple version] [detailed steps + code]

Analysis of Clickhouse mergetree principle

Tabbar settings

Kubesphere - Multi tenant management

Redhat7系统root用户密码破解
随机推荐
1. 兩數之和
The server data is all gone! Thinking caused by a RAID5 crash
Method of converting GPS coordinates to Baidu map coordinates
Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology
1. Sum of two numbers
There is no one of the necessary magic skills PXE for old drivers to install!!!
How to create and configure ZABBIX
Synthetic keyword and NBAC mechanism
Merge and migrate data from small data volume, sub database and sub table Mysql to tidb
最大似然估计,散度,交叉熵
Oauth2.0 - explanation of simplified mode, password mode and client mode
Oauth2.0 - user defined mode authorization - SMS verification code login
Mysql database table export and import with binary
ThreadLocal的简单理解
Phpstudy setting items can be accessed by other computers on the LAN
[video of Teacher Zhao Yuqiang's speech on wot] redis high performance cache and persistence
[teacher Zhao Yuqiang] Alibaba cloud big data ACP certified Alibaba big data product system
Kubernetes notes (IX) kubernetes application encapsulation and expansion
[teacher Zhao Yuqiang] calculate aggregation using MapReduce in mongodb
Project summary --2 (basic use of jsup)