开发者

Property settings save is not working properly

I have a windows app that takes advantage of the Settings. I have a few user settings that the user can set manually, they are set as User scope. The save method works fine, however if the application is closed and then reopened, the values are set back to the defaults.

here is one example of my code to save:

Properti开发者_开发百科es.Settings.Default["LocalDefaultPrinter"] = Default_Printer;
Properties.Settings.Default.Save();

what could be the problem?


I'm not sure if this is the problem, but I'm used to doing it using the designer generated properties:

Properties.Settings.Default.LocalDefaultPrinter = Default_Printer;
Properties.Settings.Default.Save();

EDIT: Well, that's not the problem - I was able to save fine using your approach provided the setting was "User" scoped. However, if you scope this as 'Application', you won't be able to save the value because that gets stored in your application.config file (application properties don't get saved back to that file on .Save).


When you alter user settings programmatically, you aren't altering the application config file. You are altering a copy of those settings in the user's profile that get merged with the application config at runtime.

Have you checked the user's profile folder for your altered settings? Are they there? If so, your code is functioning properly.

Thanks. -Jason


In addition to these possibilities, understand that user settings are scoped to the version number of the executable. So, if you automatically increment your build or change your build number during debugging, you will lose any settings already updated by a prior version of the application.


For anyone having problems in WPF.

Call the Settings.Default.Upgrade() to retrieve the non-default values when you want to use the settings you saved.


Nevermind everyone. I forgot I had a method in the code that checked if the user set printer was the same as the system set printer and if not then change it to the system printer. Took that method out and all is well.


Application scoped settings are read only whereas user scoped settings are read/write. Calling Save() will persist the user scoped settings between application sessions. If LocalDefaultPrinter is application scoped this will not persist between sessions.

This post provides information on how to make use of the settings in its entirety.


I just experienced a similar problem with one specific key in user settings. To solve it, I tried deleting the key and adding it back. Final solution, I added a new key with a new name it worked. Dumb solution but it worked.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜