views:

476

answers:

3

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?

A: 

Have you done an IISReset?

Also, there's the Microsoft.NET cache located in

WINDOWS\Microsoft.NET\Framework\vXXXXX\Temporary ASP.NET Files.

I would delete this folder's data.

Faruz
Please note that this is not an asp.net application. It's a windows application and doesn't have anything to do with IIS.
Maysam
+1  A: 

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
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?!
Maysam
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
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.
Maysam
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
Ok, let me try that and I will let you know.
Maysam
I should've changed the .exe.config file :)
Maysam
+1  A: 

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 it with the .config extension when looking for the default configuration.

Sam Holder
Excellent point.
ZombieSheep
You are right! This is the file I should have changed! :)Thank you!
Maysam
I always wondered why app.config is not in the output directory after I compile the project! and I used to copy it into the output directory myself :D I didn't know it would change to <exename>.exe.config --- Thanks again for the great point :)
Maysam