当前位置: 代码迷 >> C# >> WPF 读取Config配置文件的有关问题
  详细解决方案

WPF 读取Config配置文件的有关问题

热度:392   发布时间:2016-05-05 05:03:16.0
WPF 读取Config配置文件的问题
配置文件如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key ="Data Source" value ="Data Source=ca.db;"/>
  </appSettings>
</configuration>

我在类里面使用下面语句获取,但返回值是Null。
string constr = ConfigurationManager.AppSettings["Data Source"];


另外测试了更新提示空值出错,应该是未读取到文档
Configuration cfa = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
                cfa.AppSettings.Settings["Data Source"].Value = "db.db";
                cfa.Save();
                ConfigurationManager.RefreshSection("appSettings");


请问WPF下,应该如何添加配置文件,并可以进行读写。
------解决思路----------------------
右键点击工程,菜单中执行Add -> New Item...  选中Application  Configuration File. 

工程里的新文件名字是App.config

编译后,Exe同目录的config的名字是<output filename>.exe.config. 


修改App.config后要编译,才能把修改后的内容copy到中去。
  相关解决方案