4

I am using an app.config file to store the dynamic parameters of my application. The problem is, when I change a value in app.config file, and start the application, it doesn't load the new value from config file. Seems like the values in app.config file are being read and embedded in exe file only at compile time!

This is how I read the config file:

public class Helper
{
    static Helper()
    {
        Foo = ConfigurationManager.AppSettings["Foo"];
    }
    public static string Foo { get; set; }
}

Am I missing something?

Charles Sprayberry
  • 7,741
  • 3
  • 41
  • 50
B Faley
  • 17,120
  • 43
  • 133
  • 223

2 Answers2

24

Are you sure you are changing the correct file? You don't want to change the app.config file, but the <exename>.exe.config file, in the same directory as the .exe

The app.config file is what you edit in the ide, but when you compile your app this file is renamed to <exename>.exe.config and copied to the output directory when you compile. The .exe looks for a file with the same name as itself with the .config extension when looking for the default configuration.

Sam Holder
  • 32,535
  • 13
  • 101
  • 181
1

The static nature of your class and method may be causing you the issue. Maybe refactor it to the following...

public static class Helper
{
    public static string Foo 
    { 
        get
        {
            return ConfigurationManager.AppSettings["Foo"];
        }
    }
}

Actually, thinking about it, it doesn't help you a great deal since ConfigurationManager.AppSettings["Foo"] is already (effectively) a static call - you're just adding another layer of abstraction that may well not be required.

ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
  • Yes you are right, I guess it might be due to the nature of static properties. Just three poinsts: 1) I made it static so that it doesn't get read from config file each time I read the property. 2) My approach works greatly in web.config file in web apps 3) In case it's the problem of static property, where are the values kept after I close the application and start it again?! – B Faley Dec 16 '09 at 11:06
  • They're stored in the config file. The original question has a setter in the member, but IIRC you can't set app.config paramteers from code (unless the behaviour changed and I missed it) – ZombieSheep Dec 16 '09 at 11:10
  • I am not trying to change the value in the app.config file from code. I am just changing the value by hand. I just don't know why this change is not reflected the next time I run the application. Even if I rename or remove the app.config file, the exe file runs greatly! as if there is no need to read the config file again. As I said, seems like the key values in app.config file are only read in compile time and get embedded in exe file. – B Faley Dec 16 '09 at 11:17
  • No, they aren't compiled. Try changin your codebase to use `ConfigurationManager.AppSettings["Foo"]` each time and verify the behaviour. If (when) it works like that, you'll know the problem is within your Helper class. – ZombieSheep Dec 16 '09 at 11:34
  • Ok, let me try that and I will let you know. – B Faley Dec 16 '09 at 11:38
  • I should've changed the .exe.config file :) – B Faley Dec 16 '09 at 11:43