Changes in App.config do not reflect after restart

2019-06-27 15:49发布

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?

3条回答
来,给爷笑一个
2楼-- · 2019-06-27 16:11

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楼-- · 2019-06-27 16:25

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.

查看更多
Evening l夕情丶
4楼-- · 2019-06-27 16:33

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.

查看更多
登录 后发表回答