.NET ConfigurationManager app.config混乱 [英] .NET ConfigurationManager app.config confusion
问题描述
我有一个app.config文件,其中包含以下
I've got an app.config file, which contains the following
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name ="PowershellSnapIns" type ="System.Configuration.DictionarySectionHandler,System"/>
</configSections>
<PowershellSnapIns>
<add key="SnapIn1" value="WebAdministration" />
<add key="SnapIn2" value="Jimmy Hendrix" />
<add key="SnapIn3" value="..." />
</PowershellSnapIns>
</configuration>
我将使用ConfigurationSettings类来读取它,但是已经被弃用。这是相当简单的使用。现在我必须使用ConfigurationManager类,现在我有这个代码来读取它。
I was going to use the ConfigurationSettings class to read it, but that has been deprecated. That was fairly simple to use. Now I have to use the ConfigurationManager class, and I now have this code to read it.
System.Configuration.Configuration config =
ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
IDictionary SnapInList = (IDictionary) config.GetSection("PowershellSnapIns");
但它保持错误。我改变了app.config属性来复制到构建,但它仍然接受它找不到文件。异常说它正在寻找
TestConsole.vshost.exe.config
。 vs2k8sp1现在自动为您重命名app.config,如果是这样,我做错了什么?当然我不需要重命名app.config文件来调试vhost。我知道在发布,它可能是更名为 TestConsole.exe.config
。那么怎么了?是这样的情况下的代码错了什么?
But it keeps erroring out. I changed the app.config properties to copy across to the build, but it keeps accepting that it can't find the file. The exception says it's looking for
TestConsole.vshost.exe.config
. Does vs2k8sp1 now rename the app.config for you automatically, and if so, what am I doing wrong? Surely I don't need to rename the app.config file to debug vhost. I do know in release that it's probably being renamed TestConsole.exe.config
. So what's going wrong? Is it the case of the code wrong or what?
推荐答案
以下是我的操作方式:
系统
和 System.Configuration.dll
在您的bin目录中可用。您可以通过添加对这两个dll的引用并将其 copy local
属性设置为 true
。
-Make sure that System
and System.Configuration.dll
are available in your bin directory. You do this by adding the reference to both dlls and set their copy local
attribute to true
.
- 确保您的 App.Config
文件中,设置 code>属性为
false
。
-Make sure in your App.Config
file, set the copy local
attributes to false
.
- 使用以下方法:
-Use the following method:
public static string XMLCheck
{
get
{
var section =(Hashtable)ConfigurationManager.GetSection("PowershellSnapIns");
return (string)section["SnapIn1"];
}
}
- XMLCheck
应返回 Web管理
这篇关于.NET ConfigurationManager app.config混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!