Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 404
Chris Doggett Avatar asked Aug 03 '09 15:08

Chris Doggett


People also ask

Why appSettings keys in web config are not case sensitive?

Why appsettings keys (in web. config) are not case sensitive ? The default comparer is a CaseInsensitiveComparer that uses the conventions of the invariant culture; that is, key comparisons are case-insensitive by default.

How do I override ConfigurationManager appSettings?

It appears there is a way to do this in . NET 3.5 by setting the allowOverride attribute in the appSettings definition section of machine. config. This allows you to override the entire section in your own app.

How do I access machine config file?

Open the Machine. config file in a text editor such as Notepad. The Machine. config file is located in the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\ directory.


1 Answers

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.

like image 55
Chris Doggett Avatar answered Sep 28 '22 06:09

Chris Doggett