开发者

Changes in App.config do not reflect after restarting the application

开发者 https://www.devze.com 2022-12-14 07:55 出处:网络
I am using a开发者_StackOverflow社区n 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\'

I am using a开发者_StackOverflow社区n 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消