在 Vista 中,无法从 machine.config 读取 appSettings
我最近升级到 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用以下代码行解决了这个问题:
返回:
而不是:
忘记我现在使用的是 64 位。 在正确的配置文件中添加 appSettings 部分解决了该问题。
Solved it with the following line of code:
which returned:
instead of:
Forgot I'm using 64 bits now. Adding the appSettings section in the proper config file solved the problem.