我最近上升到Vista 64位,突然,我的machine.config中的appSettings块没有被任何.NET程序集读取。

右configSections后,和configProtectedData用C之前:\的Windows \ Microsoft.NET \框架\ V2.0.50727 \ CONFIG \ machine.config中,我有:

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

假如运行记事本++作为管理员保存它,因为它是锁着的,否则,可能是很好的理由。在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部分在适当的配置文件解决了这个问题。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top