Changes in App.config do not reflect after restart

2019-06-27 15:43发布

问题:

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?

回答1:

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.



回答2:

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.



回答3:

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.