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

边栏推荐
- Apple submitted the new MAC model to the regulatory database before the spring conference
- Oauth2.0 - using JWT to replace token and JWT content enhancement
- Sorry, this user does not exist!
- Pytorch builds the simplest version of neural network
- The most responsible command line beautification tutorial
- .NET程序配置文件操作(ini,cfg,config)
- 卷积神经网络CNN中的卷积操作详解
- 1. 两数之和
- Code generator - single table query crud - generator
- 【系统设计】邻近服务
猜你喜欢
![[teacher Zhao Yuqiang] use the catalog database of Oracle](/img/0b/73a7d12caf955dff17480a907234ad.jpg)
[teacher Zhao Yuqiang] use the catalog database of Oracle

SVN分支管理

Skywalking8.7 source code analysis (II): Custom agent, service loading, witness component version identification, transform workflow

Oauth2.0 - Introduction and use and explanation of authorization code mode

Kubernetes notes (IV) kubernetes network

Kubernetes notes (10) kubernetes Monitoring & debugging

卷积神经网络CNN中的卷积操作详解
![[teacher Zhao Yuqiang] RDB persistence of redis](/img/cc/5509b62756dddc6e5d4facbc6a7c5f.jpg)
[teacher Zhao Yuqiang] RDB persistence of redis

Code generator - single table query crud - generator

Multithreading and high concurrency (7) -- from reentrantlock to AQS source code (20000 words, one understanding AQS)
随机推荐
Cesium entity(entities) 实体删除方法
CAD插件的安装和自动加载dll、arx
理解 期望(均值/估计值)和方差
[teacher Zhao Yuqiang] MySQL high availability architecture: MHA
1. 两数之和
Mysql database table export and import with binary
Intel's new GPU patent shows that its graphics card products will use MCM Packaging Technology
Alibaba cloud Alipay sandbox payment
1. 兩數之和
JDBC connection database steps
Get a screenshot of a uiscrollview, including off screen parts
Method of converting GPS coordinates to Baidu map coordinates
Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
Cesium 点击获三维坐标(经纬度高程)
arcgis创建postgre企业级数据库
Solve the problem of automatic disconnection of SecureCRT timeout connection
[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
Bernoulli distribution, binomial distribution and Poisson distribution, and the relationship between maximum likelihood (incomplete)
[teacher Zhao Yuqiang] the most detailed introduction to PostgreSQL architecture in history
The server data is all gone! Thinking caused by a RAID5 crash