WPF中config配置文件的使用说明
一、软件版本说明
使用系统是WIndows7 SP1 64位,开发软件是visual stdio 2019,选择框架net framework4.7.2。
二、config文件用途
WPF中config文件是应用程序中经常使用的一种配置文件,System.Configuration.dll文件中提供了大量的读写的配置,所以它是一种高效的程序配置方式。可以将程序中经常用到的参数写入配置文件中,这样方便调用和修改。
在项目根目录下会有app.config,而在项目bin/debug目录下有*.exe.config。每一次编译生成exe时,app.config文件的内容会自动同步到exe.Config,而在程序运行时,程序内修改的config只会被保存到exe.config,而不会保存到app.config。
三、config文件格式
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
</runtime>
<appSettings>
<add key="DBIP" value="127.0.0.1"/>
</appSettings>
</configuration>
config文件中包含系统默认的根节点configuration、系统默认的子节点appSettings,可以在appSettings中添加参数:
add ——系统关键字,表示添加一行
key ——系统关键字,表示您需要配置的变量名字
value ——系统关键字,表示该变量的值
也可以自定义section,但需要在中声明,如不声明,会报System.TypeInitializationException:““System.Windows.Application”的类型初始值设定项引发异常。”错误。
如下所示定义MySettings。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="MySettings" type="System.Configuration.DictionarySectionHandler"/>
</configSections>
<MySettings>
<add key="hello" value="world"/>
</MySettings>
</configuration>
自定义section需声明name和type,name很好理解,type声明类型,这里的类型规定了后面对它进行读写所使用的函数方法,常用的有ystem.Configuration.SingleTagSectionHandler、 System.Configuration.DictionarySectionHandler、 System.Configuration.NameValueSectionHandler
也可以自定义类型,如type=“MyClass.MyFunction”,这样在MyClass类中编写一个函数MyFunction,来读写section,MyClass类继承继承于IConfigurationSectionHandler。
四、config文件读写
config文件格式是XML语言格式,所以对其读写可以使用System.Xml,也可以使用System.Configuration。但是使用System.Configuration只能读写默认节点appSettings,而读写自定义节点会很不方便。
4.1读写默认节点
见下面代码,已加注释,需要注意的是先对System.Configuration进行引用,保存要进行刷新,并且,更改的是exe.config文件。
string tmp01 = ConfigurationManager.AppSettings["DBIP"];//读取key=DBIP的value
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
cfa.AppSettings.Settings["DBIP"].Value = “127.0.0.1”;//设置key=DBIP的value
cfa.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");//保存修改并刷新
4.2读写自定义节点
这里使用xml进行读写自定义节点,先使用Configuration读取config文件路径,然后载入xml中。
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var section = cfa.GetSection("MySettings");
var section01 = section.CurrentConfiguration.GetSection("MySettings");
string filepath = section01.CurrentConfiguration.FilePath;
XmlDocument xmldoc = new XmlDocument();
xmldoc.Load(filepath);
读取xml自定义节点,通过循环读取节点下子节点。
XmlNode node = xmldoc.SelectSingleNode("configuration/MySettings");
foreach (XmlNode item in node.ChildNodes)
{
string tmp01 = item.Attributes["key"].Value;
string tmp02 = item.Attributes["value"].Value;
scalelist.Add(tmp01);
scaledict.Add(tmp01, tmp02);
}
编辑自定义节点,可直接对Attributes[“key”]和Attributes[“value”]赋值,如需创建子节点,见下方代码。
string value = value01 + value02;
XmlNode node = xmldoc.SelectSingleNode("configuration/MySettings");
XmlElement xmlelement = xmldoc.CreateElement("add");
xmlelement.SetAttribute("key", key);
xmlelement.SetAttribute("value", value);
node.AppendChild(xmlelement);
xmldoc.Save(filepath);
参考文档
Xml文档添加节点和属性
关于ConfigurationManager.GetSection()方法
WPF App.config配置文件的使用说明
WPF程序中App.Config文件的读与写