gpt4 book ai didi

winforms - 未从 Vista 中的 machine.config 读取 appSettings

转载 作者:行者123 更新时间:2023-12-04 14:37:00 26 4
gpt4 key购买 nike

我最近升级到了 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 中那样工作的东西。

最佳答案

用以下代码行解决了它:

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 部分解决了这个问题。

关于winforms - 未从 Vista 中的 machine.config 读取 appSettings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1222909/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com