winforms.net-2.0vista64machine.config

appSettings not being read from machine.config in Vista


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.


Solution

  • Solved it with the following line of code:

    ConfigurationManager.OpenMachineConfiguration().FilePath
    

    which returned:

    C:\Windows\Microsoft.NET\Framework64\v2.0.50727\Config\machine.config
    

    instead of:

    C:\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG\machine.config
    

    Forgot I'm using 64 bits now. Adding the appSettings section in the proper config file solved the problem.