6

最近、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 と同じように動作するはずです。

4

1 に答える 1

8

次のコード行で解決しました。

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 セクションを追加すると、問題が解決しました。

于 2009-08-03T15:46:58.673 に答える