当前位置:网站首页>. 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
边栏推荐
- Understand one-way hash function
- [teacher Zhao Yuqiang] use Oracle's tracking file
- [teacher Zhao Yuqiang] redis's slow query log
- Project summary --01 (addition, deletion, modification and query of interfaces; use of multithreading)
- pytorch DataLoader实现miniBatch(未完成)
- CKA certification notes - CKA certification experience post
- JS implements the problem of closing the current child window and refreshing the parent window
- Kubesphere - build Nacos cluster
- ThreadLocal的简单理解
- Cesium entity(entities) 实体删除方法
猜你喜欢
Synthetic keyword and NBAC mechanism
Phpstudy setting items can be accessed by other computers on the LAN
ThreadLocal的简单理解
Exception when introducing redistemplate: noclassdeffounderror: com/fasterxml/jackson/core/jsonprocessingexception
Jedis source code analysis (I): jedis introduction, jedis module source code analysis
[teacher Zhao Yuqiang] Alibaba cloud big data ACP certified Alibaba big data product system
Why is the website slow to open?
Bernoulli distribution, binomial distribution and Poisson distribution, and the relationship between maximum likelihood (incomplete)
Oauth2.0 - explanation of simplified mode, password mode and client mode
Es remote cluster configuration and cross cluster search
随机推荐
Zhiniu stock project -- 05
If function of MySQL
从小数据量分库分表 MySQL 合并迁移数据到 TiDB
1. Somme des deux nombres
理解 YOLOV1 第一篇 预测阶段
Multithreading and high concurrency (7) -- from reentrantlock to AQS source code (20000 words, one understanding AQS)
[teacher Zhao Yuqiang] index in mongodb (Part 2)
Txt document download save as solution
Alibaba cloud Alipay sandbox payment
Installation du plug - in CAD et chargement automatique DLL, Arx
PMP笔记记录
Bio, NiO, AIO details
[teacher Zhao Yuqiang] index in mongodb (Part 1)
1. 兩數之和
BeanDefinitionRegistryPostProcessor
Jackson: what if there is a lack of property- Jackson: What happens if a property is missing?
Bernoulli distribution, binomial distribution and Poisson distribution, and the relationship between maximum likelihood (incomplete)
卷积神经网络CNN中的卷积操作详解
[teacher Zhao Yuqiang] kubernetes' probe
Btrfs and ext4 - features, strengths and weaknesses