.NET ConfigurationManager中的app.config混乱(.NET Confi

2019-09-23 17:52发布

我有一个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>

我将使用ConfigurationSettings类读取它,但这已被弃用。 这是使用相当简单。 现在,我必须使用ConfigurationManager中类,我现在有这样的代码读取它。

 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文件调试虚拟主机。 我知道在新闻稿中它可能被改名TestConsole.exe.config 。 那么是什么回事? 难道是代码错误或什么情况下?

Answer 1:

以下是我做的:

-确保该SystemSystem.Configuration.dll在你的bin目录中。 您可以通过添加引用的DLL都做到这一点,他们的设置copy local属性为true

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

- 使用下面的方法:

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

    }
}

-The XMLCheck应该返回“ WebAdministration



Answer 2:

VS重命名的app.config到yourappname.exe,并与您的.exe一起把它放到bin目录

你能确认文件的bin目录中有你的EXE一起。



文章来源: .NET ConfigurationManager app.config confusion