Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are persisted user settings not loaded?

I have a windows application that uses an assembly that stores some configuration settings in the default application settings.

The settings can be changed in run time and are persisted thus:

Properties.Settings.Default.SelectedCOMPort = options.SelectedCOMPort;
Properties.Settings.Default.Save();

The settings are saved correctly and I confirm this by looking at the user.config file saved in the users application directory E.g.

C:\Documents and Settings\e399536\Local Settings\Application Data\MyCompany\MyTool

However when the tool is closed and then started again all the settings are loaded with their default values.

Checking the user.config file once the application is running confirms that the settings are still as saved.

The settings are loaded thus:

options.SelectedCOMPort = Properties.Settings.Default.SelectedCOMPort;

Why are the default settings being used and not the saved ones?

Have I missed something??

@ Tenaciouslmpy The settings are loaded during the constructor of the assembly, which itself is loaded in the form load event of the main assembly.

@ Austin This is a standalone app that I am debugging in Visual Studio.

like image 945
Kildareflare Avatar asked Feb 04 '10 17:02

Kildareflare


People also ask

How do I persist changes to the user settings?

You can use the My.Settings.Save method to persist changes to the user settings. Typically, applications are designed to persist the changes to the user settings when the application shuts down. This is because saving the settings can take, depending on several factors, several seconds.

What does'user profile cannot be loaded'mean?

Logging in to a computer is as easy as typing a password (if any) and tapping the sign-in button. If your PC refuses to load your profile and there's a blue screen with a 'User profile cannot be loaded' error message on it, then there's a problem with some files or services that powers your PC's user profile.

How to persist user settings in Visual Basic?

How to: Persist User Settings in Visual Basic. You can use the My.Settings.Save method to persist changes to the user settings. Typically, applications are designed to persist the changes to the user settings when the application shuts down. This is because saving the settings can take, depending on several factors, several seconds.

How to manage persistent user state in your ASP NET application?

Nine Options for Managing Persistent User State in Your ASP.NET Application ASP.NET provides many different ways to persist data between user requests. You can use the Application object, cookies, hidden fields, the Session or Cache objects, and lots of other methods. Deciding when to use each of these can sometimes be difficult.


2 Answers

If you are recompiling the application between runs, note that it will consider that a new version of the app and will not automatically load per-user settings. You need to call Settings.Default.Upgrade in this situation.

One way to do this only when needed is to add a NeedsUpgrade setting (value True) to the application's default per-user settings. On app startup, check if NeedsUpgrade is true. If so, call Upgrade, set NeedsUpgrade to False, and save the settings. The next time the app version changes, NeedsUpgrade will reset to True and you'll auto-call Upgrade to bring in any existing user settings again.

Make sure you're setting NeedsUpgrade after calling Upgrade, or it will get wiped out when the settings are upgraded.

if (Settings.Default.NeedsUpgrade)
{
    Settings.Default.Upgrade();
    Settings.Default.NeedsUpgrade = false;
    Settings.Default.Save();
}
like image 67
technophile Avatar answered Sep 25 '22 02:09

technophile


This sounds like you're debugging the application from Visual Studio when every time you start a new session you start with the default data.

If you're seeing this with an installed release, then I would guess you're not actually using the string values when you think you are.

like image 28
Austin Salonen Avatar answered Sep 23 '22 02:09

Austin Salonen