appSettingsがVistaのmachine.configから読み取られない
-
10-07-2019 - |
質問
最近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()
---
作成するアプリは、app.configで見つからない場合にユーザーが実行する環境を見つけるためのフォールバックとしてmachine.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セクションを追加すると、問題が解決しました。
所属していません StackOverflow