【问题标题】:Read config files in a WPF project读取 WPF 项目中的配置文件
【发布时间】:2011-05-09 10:36:11
【问题描述】:

我有一个 WPF 应用程序。这个应用程序应该读取一个 web.config 文件(来自一个 asp.net webforms 项目)和一个 myCompany.config 文件。 我怎样才能做到这一点?有没有简单的方法来处理这个问题?

任何帮助将不胜感激。

谢谢!

【问题讨论】:

  • 很遗憾,您不能在 wpf 应用程序中使用 webconfigurationmanager,因为它存在于 system.web dll 中,如果您想访问配置文件中的基本信息,您可以使用 XDocument(Linq to Xml 等)因为配置文件是普通的 Xml

标签: c# wpf web-config configuration-files


【解决方案1】:

【讨论】:

  • Thx,但是如何使用 ConfigurationManager 设置配置文件的路径?这两个文件位于不同的位置。
【解决方案2】:

如果我理解正确的话,你想读取存储在特定路径中的配置文件吗?

这是诀窍:

Configuration _userConfig;
String configFilePath = "Your path here";

//Getting the user configuration object:
//Mapping the appropriate path:
ExeConfigurationFileMap configFileMap = new ExeConfigurationFileMap();
configFileMap.ExeConfigFilename = configFilePath;
_userConfig = ConfigurationManager.OpenMappedExeConfiguration(configFileMap, ConfigurationUserLevel.None);

然后,_userConfig 将在您指定的路径中包含详细信息:)

【讨论】:

  • 我更喜欢将它们包含在此处指出的 web.config 中的方法:stackoverflow.com/questions/1061999/…
  • 事实上,我在 WPF/C# 应用程序中将此过程与 App.config 文件一起使用。我不能确保它实际上是在使用 web.config,您可能更喜欢使用 Kaido 的方式。我使用这种面向代码的方式的目的是动态解析路径(每个用户在存储在他的环境变量之一的文件夹中都有一个配置文件)。如果你有一个固定的路径,你可能想考虑另一种选择,但这个方法无论如何都可以使用App.config
猜你喜欢
  • 2012-09-02
  • 2014-07-16
  • 1970-01-01
  • 1970-01-01
  • 2015-09-26
  • 2019-11-05
  • 2012-05-15
  • 2020-12-04
  • 2015-04-02
相关资源
最近更新 更多