Settings.settings File Keeps Getting Reset

后端 未结 4 557
隐瞒了意图╮
隐瞒了意图╮ 2020-12-16 03:40

When debugging my project in Visual Studio 2008, my Settings.settings file keeps getting reset between builds. Is there a way to prevent this from happening?

Thank

相关标签:
4条回答
  • 2020-12-16 03:42

    Okay, I found out the answer I was really looking for. Basically, you need to call LocalFileSettingsProvider.Upgrade. However, since I will be deploying using ClickOnce, it will do it for you automatically.


    Q: Okay, but how do I know when to call Upgrade?

    A: Good question. In Clickonce, when you install a new version of your application, ApplicationSettingsBase will detect it and automatically upgrade settings for you at the point settings are loaded. In non-Clickonce cases, there is no automatic upgrade - you have to call Upgrade yourself. Here is one idea for determining when to call Upgrade:

    Have a boolean setting called CallUpgrade and give it a default value of true. When your app starts up, you can do something like:

    if (Properties.Settings.Value.CallUpgrade)
    {
        Properties.Settings.Value.Upgrade();
        Properties.Settings.Value.CallUpgrade = false;
    }
    

    This will ensure that Upgrade() is called only the first time the application runs after a new version is deployed.

    REF: http://blogs.msdn.com/rprabhu/articles/433979.aspx

    0 讨论(0)
  • 2020-12-16 03:49

    Amongst other reasons, the Settings file keeps getting reset each time you Debug simply because the next time you Debug, you'll be able to test the whole application all over again. Not resetting the Settings may lead to undetected bugs.

    0 讨论(0)
  • 2020-12-16 04:05

    I believe Settings.settings files are saved based on the current version number, basically as a "feature" where settings are not saved between differing versions of the same program on a machine. Assuming you're incrementing the version number automatically when compiling (1.0.* in AssemblyInfo.cs), you'll be resetting your settings everytime you compile a new version.

    To correct this, the best course would be to serialize your own settings file to the Application Data directory.

    0 讨论(0)
  • 2020-12-16 04:07

    Off the top of my head I think you can set in the properties of the file (in Visual Studio right click, Properties) an option to "do not copy" when the project is built/run. What is happening probably is when your project is built, the settings file is copied to the debug bin directory, overwriting the settings file from the previous run.

    0 讨论(0)
提交回复
热议问题