C# .NET ConfigurationManager app.config 混淆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1278730/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 14:36:29  来源:igfitidea点击:

.NET ConfigurationManager app.config confusion

c#configurationapp-config

提问by scope_creep

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. That was fairly simple to use. Now I have to use the ConfigurationManager class, and I now have this code to read it.

我打算使用 ConfigurationSettings 类来读取它,但它已被弃用。使用起来相当简单。现在我必须使用 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. 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?

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

采纳答案by Graviton

Here's how I did it:

这是我如何做到的:

-Make sure that Systemand System.Configuration.dllare available in your bin directory. You do this by adding the reference to both dlls and set their copy localattribute to true.

- 确保SystemSystem.Configuration.dll在您的 bin 目录中可用。您可以通过添加对两个 dll 的引用并将它们的copy local属性设置为true.

-Make sure in your App.Configfile, set the copy localattributes to false.

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

-Use the following method:

- 使用以下方法:

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

    }
}

-The XMLCheckshould return "WebAdministration"

-XMLCheck应该返回“ WebAdministration

回答by jkelley

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 目录中。