我最近升级到了Vista x64,突然间,我的machine.config appSettings块不再被任何.NET程序集读取。
在C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config,中,在configSections之后和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相同的工作方式。
发布于 2009-08-03 15:46:59
用下面这行代码解决了这个问题:
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部分解决了这个问题。
https://stackoverflow.com/questions/1222909
复制相似问题