陕西煤业化工建设集团有限公司网站,网络优化和推广,网站建设的公司哪家强,多好商城下载#x1f680; 优质资源分享 #x1f680;
学习路线指引#xff08;点击解锁#xff09;知识定位人群定位#x1f9e1; Python实战微信订餐小程序 #x1f9e1;进阶级本课程是python flask微信小程序的完美结合#xff0c;从项目搭建到腾讯云部署上线#xff0c;打造一… 优质资源分享
学习路线指引点击解锁知识定位人群定位 Python实战微信订餐小程序 进阶级本课程是python flask微信小程序的完美结合从项目搭建到腾讯云部署上线打造一个全栈订餐系统。Python量化交易实战入门级手把手带你打造一个易扩展、更安全、效率更高的量化交易系统
在程序开发过程中我们一般会用到配置文件来设定一些参数。常见的配置文件格式为 ini, xml, config等。
INI
.ini文件通常为初始化文件是用来存储程序配置信息的文本文件。
[Login]
#开启加密 0:不开启、1:开启
open\_ssl\_certificate0
.NET 框架本身不支持 INI 文件可以利用 Windows API方法使用平台调用服务来写入和读取文件。
// 要写入的部分名称 - sectionName
// 要设置的键名 - key
// 要设置的值 - value
// INI文件位置 - filepath
// 读取是否成功 - result
[DllImport(kernel32)]
bool WritePrivateProfileString(string sectionName,string key,string value,string filepath);// 要读取的部分名称 - sectionName
// 要读取的键名 - key
// 如果键不存在返回的默认值 - default
// 接收用作缓冲区的字符串 - ReturnedVal
// 实际读取的值 - maxsize
// INI文件位置 - filepath
[DllImport(kernel32)]
int GetPrivateProfileString(string sectionName,string key,string default,StringBuilder ReturnedVal,int maxsize,string filepath);
一般会封装一个类来调用该API方法。
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 是 .NET 的CFG/INI 配置文件操作组件以文本或二进制格式读取修改和保存配置文件和流。
Configuration config Configuration.LoadFromFile(login.cfg);
Section section config[Login];
// 读取参数
bool isOpen section[open\_ssl\_certificate].GetValuebool();
// 修改参数
section[open\_ssl\_certificate].Value false;
Config
在 App.config/web.config 文件中的 configSections 节点下配置 section 节点.NET 提供自带的类型进行封装。 configSections节点必须为configuration下第一个节点。
NameValue键值对
xml version1.0 encodingutf-8 ?
configuration configSections section nameNameValueConfigNode typeSystem.Configuration.NameValueSectionHandler/ configSections NameValueConfigNode add keyName一 valueValue一 / add keyName二 valueValue二 / NameValueConfigNode startup supportedRuntime versionv4.0 sku.NETFramework,Versionv4.6 / startup
configuration
定义一个静态属性的方法获取 Dictionary 格式的数据
///
/// NameValueCollection
///
public static Dictionarystring, string NameValueConfigNode
{get{NameValueCollection nvc (NameValueCollection)ConfigurationManager.GetSection(NameValueConfigNode);Dictionarystring, string result new Dictionarystring,string();foreach (string key in nvc.AllKeys){ result.Add(key, nvc[key]);}return result;}
}
Dictionary
xml version1.0 encodingutf-8 ?
configurationconfigSectionssection nameDictionaryConfigNode typeSystem.Configuration.DictionarySectionHandler/configSectionsDictionaryConfigNodeadd keyKey一 valueDictValue一 /add keyKey二 valueDictValue二 /DictionaryConfigNodestartup supportedRuntime versionv4.0 sku.NETFramework,Versionv4.6 /startup
configuration
///
/// Dictionary
///
public static Dictionarystring, string DictionaryConfigNode
{get{IDictionary dict (IDictionary)ConfigurationManager.GetSection(DictionaryConfigNode);Dictionarystring, string result new Dictionarystring, string();foreach (string key in dict.Keys){result.Add(key, dict[key].ToString());}return result;}
}
SingTag
xml version1.0 encodingutf-8 ?
configurationconfigSectionssection nameSingleTagConfigNode typeSystem.Configuration.SingleTagSectionHandler /configSectionsSingleTagConfigNode PropertyOne1 PropertyTwo2 PropertyThree3 PropertyFour4 PropertyFive5 /startup supportedRuntime versionv4.0 sku.NETFramework,Versionv4.6 /startup
configuration
///
/// SingleTag
///
public static Dictionarystring, string SingleTagConfigNode
{get{Hashtable dict (Hashtable)ConfigurationManager.GetSection(SingleTagConfigNode);Dictionarystring, string result new Dictionarystring, string();foreach (string key in dict.Keys){result.Add(key, dict[key].ToString());}return result;}
}
自定义配置文件
如果配置文件很多可以单独定义配置文件然后在 App.config/Web.config 文件中声明。
xml version1.0 encodingutf-8 ?
configurationconfigSectionssection nameMyConfigData1 typeConsoleApplication.ConfigFiles.ConfigFile,ConsoleApplication/configSectionsMyConfigData configSourceConfigFiles\MyConfigFile.config/startup supportedRuntime versionv4.0 sku.NETFramework,Versionv4.6 /startup
configuration
自定义文件 MyConfigFile.config 内容
xml version1.0 encodingutf-8 ?
MyConfigDataadd keyKey一 value自定义文件一 /add keyKey二 value自定义文件二 /add keyKey三 value自定义文件三 /
MyConfigData
XML
XML文件常用于简化数据的存储和共享它的设计宗旨是传输数据而非显示数据。对于复杂不规则的配置信息也可以用XML文件进行存储。
// 读取文件
XmlDocument xmlDoc new XmlDocument();
xmlDoc.Load(myfile.xml);
// 根节点
var nodeRoot xmlDoc.DocumentElement;
// 创建新节点
XmlElement studentNode xmlDoc.CreateElement(student);
// 创建新节点的孩子节点
XmlElement nameNode xmlDoc.CreateElement(name);
// 建立父子关系
studentNode.AppendChild(nameNode);
nodeRoot.AppendChild(studentNode);
XML基础教程https://www.w3school.com.cn/xml/index.asp
我的公众号