简体   繁体   English

.NET ConfigurationManager app.config混乱

[英].NET ConfigurationManager app.config confusion

I've got an app.config file, which contains the following 我有一个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>

I was going to use the ConfigurationSettings class to read it, but that has been deprecated. 我打算使用ConfigurationSettings类来读取它,但是已经弃用了。 That was fairly simple to use. 这很简单易用。 Now I have to use the ConfigurationManager class, and I now have this code to read it. 现在我必须使用ConfigurationManager类,现在我有了这段代码来读取它。

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

IDictionary SnapInList = (IDictionary) config.GetSection("PowershellSnapIns");

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. 我更改了app.config属性以复制到构建,但它一直接受它无法找到该文件。 The exception says it's looking for TestConsole.vshost.exe.config . 例外情况说它正在寻找TestConsole.vshost.exe.config Does vs2k8sp1 now rename the app.config for you automatically, and if so, what am I doing wrong? vs2k8sp1现在是否会自动为您重命名app.config,如果是这样,我做错了什么? Surely I don't need to rename the app.config file to debug vhost. 当然,我不需要将app.config文件重命名为debug vhost。 I do know in release that it's probably being renamed TestConsole.exe.config . 我在发行版中确实知道它可能正在重命名为TestConsole.exe.config So what's going wrong? 出了什么问题? Is it the case of the code wrong or what? 这是代码错误的情况还是什么?

Here's how I did it: 我是这样做的:

-Make sure that System and System.Configuration.dll are available in your bin directory. - 确保您的bin目录中有SystemSystem.Configuration.dll可用。 You do this by adding the reference to both dlls and set their copy local attribute to true . 您可以通过添加对两个dll的引用并将其copy local属性设置为true

-Make sure in your App.Config file, set the copy local attributes to false . - 确保在App.Config文件中,将copy local attributes设置为false

-Use the following method: - 使用以下方法:

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

    }
}

-The XMLCheck should return " WebAdministration " - XMLCheck应返回“ WebAdministration

VS renames the app.config to yourappname.exe and places it in the bin directory along with your .exe VS将app.config重命名为yourappname.exe,并将其与.exe一起放在bin目录中

Can you confirm that the file exists in the bin directory along with your exe. 你可以确认该文件与你的exe一起存在于bin目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM