.NET ConfigurationManager app.config混乱

sco*_*eep 5 c# configuration app-config

我有一个app.config文件,其中包含以下内容

<?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>
Run Code Online (Sandbox Code Playgroud)

我打算使用ConfigurationSettings类来读取它,但是已经弃用了.这很简单易用.现在我必须使用ConfigurationManager类,现在我有了这段代码来读取它.

 System.Configuration.Configuration config =
     ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);

IDictionary SnapInList = (IDictionary) config.GetSection("PowershellSnapIns");
Run Code Online (Sandbox Code Playgroud)

但它不断出错.我更改了app.config属性以复制到构建,但它一直接受它无法找到该文件.例外情况说它正在寻找 TestConsole.vshost.exe.config.vs2k8sp1现在是否会自动为您重命名app.config,如果是这样,我做错了什么?当然,我不需要将app.config文件重命名为debug vhost.我在发布中确实知道它可能正在重命名TestConsole.exe.config.出了什么问题?这是代码错误的情况还是什么?

Gra*_*ton 12

我是这样做的:

- 确保您的bin目录中有SystemSystem.Configuration.dll可用.您可以通过添加对两个dll的引用并将其copy local属性设置为来执行此操作true.

- 确保在您的App.Config文件中,将copy local属性设置为false.

- 使用以下方法:

public static string XMLCheck
{
    get
    {
        var section =(Hashtable)ConfigurationManager.GetSection("PowershellSnapIns");
        return (string)section["SnapIn1"];

    }
}
Run Code Online (Sandbox Code Playgroud)

- XMLCheck应该返回" WebAdministration"