简体   繁体   English

Settings.settings文件保持重置

[英]Settings.settings File Keeps Getting Reset

When debugging my project in Visual Studio 2008, my Settings.settings file keeps getting reset between builds. 在Visual Studio 2008中调试我的项目时,我的Settings.settings文件不断在构建之间重置。 Is there a way to prevent this from happening? 有没有办法防止这种情况发生?

Thanks. 谢谢。

Okay, I found out the answer I was really looking for. 好的,我找到了我真正想要的答案。 Basically, you need to call LocalFileSettingsProvider.Upgrade. 基本上,您需要调用LocalFileSettingsProvider.Upgrade。 However, since I will be deploying using ClickOnce, it will do it for you automatically. 但是,由于我将使用ClickOnce进行部署,因此它会自动为您完成。


Q: Okay, but how do I know when to call Upgrade? 问:好的,但我如何知道何时致电升级?

A: Good question. A:好问题。 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. 在Clickonce中,当您安装新版本的应用程序时,ApplicationSettingsBase将检测它并在加载点设置时自动升级设置。 In non-Clickonce cases, there is no automatic upgrade - you have to call Upgrade yourself. 在非Clickonce案例中,没有自动升级 - 您必须自己调用升级。 Here is one idea for determining when to call Upgrade: 这是确定何时调用升级的一个想法:

Have a boolean setting called CallUpgrade and give it a default value of true. 有一个名为CallUpgrade的布尔设置,并给它一个默认值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. 这将确保仅在部署新版本后第一次运行应用程序时调用Upgrade()。

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

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. 我相信Settings.settings文件是根据当前版本号保存的,基本上是一个“功能”,其中设置不保存在机器上同一程序的不同版本之间。 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. 假设您在编译时自动增加版本号(在AssemblyInfo.cs中为1.0。*),每次编译新版本时都会重置设置。

To correct this, the best course would be to serialize your own settings file to the Application Data directory. 要纠正这个问题,最好的方法是将您自己的设置文件序列化到Application Data目录。

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. 我认为你可以设置文件的属性(在Visual Studio中右键单击,属性),在项目构建/运行时选择“不要复制”。 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. 可能发生的事情是,在构建项目时,将设置文件复制到调试bin目录,覆盖上一次运行中的设置文件。

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. 除了其他原因之外,每次调试时,Settings文件都会重置,因为下次调试时,您将能够再次测试整个应用程序。 Not resetting the Settings may lead to undetected bugs. 不重置设置可能会导致未检测到的错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM