栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

WPF中config配置文件的使用说明

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

WPF中config配置文件的使用说明

WPF中config配置文件的使用说明
  • 一、软件版本说明
  • 二、config文件用途
  • 三、config文件格式
  • 四、config文件读写
    • 4.1读写默认节点
    • 4.2读写自定义节点
  • 参考文档

一、软件版本说明

使用系统是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文件格式




  
  

  




config文件中包含系统默认的根节点configuration、系统默认的子节点appSettings,可以在appSettings中添加参数:
add ——系统关键字,表示添加一行

key ——系统关键字,表示您需要配置的变量名字

value ——系统关键字,表示该变量的值
也可以自定义section,但需要在中声明,如不声明,会报System.TypeInitializationException:““System.Windows.Application”的类型初始值设定项引发异常。”错误。
如下所示定义MySettings。



  
  

自定义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文件的读与写

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/910908.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号