views:

187

answers:

2

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.

+1  A: 

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.

Austin Salonen
Ah, that would be it, Cheers.
Kildareflare
+2  A: 

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.

technophile
I'm not recompiling however I am debugging in the IDE which seems to do the same thing. In any case the Upgrade() tip has solved the issue. Cheers.
Kildareflare