代码之家  ›  专栏  ›  技术社区  ›  Chris Doggett

未从Vista中的machine.config读取appSettings

  •  6
  • Chris Doggett  · 技术社区  · 17 年前

    我最近升级到Vista x64,突然间,我的machine.config appSettings块没有被任何人读取。NET程序集。

    在C:\Windows\Microsoft中,在configSections之后和configProtectedData之前。NET\Framework\v2.0.50727\CONFIG\machine.CONFIG,我有:

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

        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()
    ---
    

    1 回复  |  直到 17 年前
        1
  •  8
  •   Chris Doggett    17 年前

    用以下代码行解决了这个问题:

    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
    

    推荐文章