在 Vista 中,无法从 machine.config 读取 appSettings

发布于 2024-07-29 18:50:44 字数 1236 浏览 6 评论 0原文

我最近升级到 Vista x64,突然之间,我的 machine.config appSettings 块没有被任何 .NET 程序集读取。

就在 configSections 之后,在 C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config 中的 configProtectedData 之前,我有:

<appSettings>
    <add key="foo" value="blah"/>
</appSettings>
<system.runtime.remoting>
    <customErrors mode="Off"/>
</system.runtime.remoting>

必须通过以管理员身份运行 Notepad++ 来保存它,因为否则它会被锁定,可能是很好的理由。 在 SnippetCompiler 或 VS .NET 2008 中运行以下代码:

    foreach(var s in ConfigurationManager.AppSettings.AllKeys)
    {
        Console.WriteLine(s);   
    }

    AppSettingsReader asr = new AppSettingsReader();

    Console.WriteLine(asr.GetValue("foo", typeof(string)));

不写出任何键并失败,并出现以下异常:

---
The following error occurred while executing the snippet:
System.InvalidOperationException: The key 'foo' does not exist in the appSettings configuration section.
    at System.Configuration.AppSettingsReader.GetValue(String key, Type type)
    at MyClass.RunSnippet()
    at MyClass.Main()
---

我编写的应用程序使用 machine.config 作为后备,用于找出用户应该在无法运行的环境中运行可以在 app.config 中找到,所以我想避免重写我的应用程序来找出应该与 2000 和 XP 中相同的工作方式。

I've recently moved up to Vista x64, and suddenly, my machine.config appSettings block isn't being read by any .NET assemblies.

Right after configSections, and before configProtectedData in C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config, I have:

<appSettings>
    <add key="foo" value="blah"/>
</appSettings>
<system.runtime.remoting>
    <customErrors mode="Off"/>
</system.runtime.remoting>

Had to save it by running Notepad++ as an administrator, because it's locked otherwise, probably for good reasons. Running the following code in SnippetCompiler or VS .NET 2008:

    foreach(var s in ConfigurationManager.AppSettings.AllKeys)
    {
        Console.WriteLine(s);   
    }

    AppSettingsReader asr = new AppSettingsReader();

    Console.WriteLine(asr.GetValue("foo", typeof(string)));

writes out no keys and fails with the following exception:

---
The following error occurred while executing the snippet:
System.InvalidOperationException: The key 'foo' does not exist in the appSettings configuration section.
    at System.Configuration.AppSettingsReader.GetValue(String key, Type type)
    at MyClass.RunSnippet()
    at MyClass.Main()
---

The app I write uses machine.config as a fallback for finding out which environment a user should be running in if it can't be found in the app.config, so I'd like to avoid having to rewrite my app to figure out something that should be working the same as it did in 2000 and XP.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我很坚强 2024-08-05 18:50:44

使用以下代码行解决了这个问题:

ConfigurationManager.OpenMachineConfiguration().FilePath

返回:

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Config\machine.config

而不是:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config

忘记我现在使用的是 64 位。 在正确的配置文件中添加 appSettings 部分解决了该问题。

Solved it with the following line of code:

ConfigurationManager.OpenMachineConfiguration().FilePath

which returned:

C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Config\machine.config

instead of:

C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config

Forgot I'm using 64 bits now. Adding the appSettings section in the proper config file solved the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文