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

标签 winforms .net-2.0 vista64 machine.config

我最近升级到了 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/

相关文章:

unlock - "Unlocker"相当于 Vista64?

c# - 如何将 Linq 语句的结果作为 List 获取?

c# - 如何将 c# winform 应用程序与 Interactive Brokers 连接

.net - 如何确定 SqlConnection 是否被登记到 System.Transactions 的 tx 中?

c# 当从哈希表中删除元素时,哈希表如何缩小?

vba - 如何使用 VBA 从 64 位 Windows Vista 调用 ActivateKeyboardLayout

c# - 访问文件夹时,Web 浏览器控件身份验证不起作用

c# - 不能为自定义事件重载委托(delegate)?

.net - 为 XML 编码文本数据的最佳方式

mercurial - Vista 64 位中的 TortoiseHg 不显示上下文菜单