User settings - different location for each application version? - c#

I'm developing an application that saves various user settings (e.g. window locations, options, preferences). This is done using the syntax
Properties.Settings.Default.setting_name = "xxx";
Properties.Settings.Default.Save();
and
var x = Properties.Settings.Default.setting_name;
Here is an example of the underlying settings file path:-
C:\Users\user_name\AppData\Local\company_name\exe_name.vsh_Url_kxrjspzszls01bmlnkpeuf5cutfdioia\1.0.0.0
The problem is that each time I build and release a new version of the software, users are losing their settings. This is presumably down to the fact that the application exe version number is being included in the file path, so each time a new version is installed it starts over with a new, empty settings folder? Fortunately we're still in the dev phase so it's only a couple of internal users at present.
What's going on, and is there a way around it?
I'm also concerned because we have an existing product out in the field, and are due to release a new version shortly. I'm panicking that all these users will lose their settings and preferences when they upgrade.

So based on other SO questions and articles found on the web, I've written this short method that I now call during application startup:
private static void CheckForUserSettingsUpgrade()
{
if (!Properties.Settings.Default.UserSettingsUpgradeRequired)
{
return;
}
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UserSettingsUpgradeRequired = false;
Properties.Settings.Default.Save();
}
For this to work there must be a user-scope setting called UserSettingsUpgradeRequired with a default value of True.
After an upgrade (resulting in a new settings file being created) this setting's value will of course be true. In this scenario the above code will import all settings from the previous settings file (if any), set this upgrade flag setting to false, then save the changes. After the next upgrade, the flag's value will revert to true, and the process will repeat itself.

Related

Settings not saved in Settins.settings

I'm using Settings.Settings to store settings at run-time. It was very helpful for my earlier application to store data. But in my current project its not saving the settings data. My application have some tab and each tab contains some TextBox. Im using textBox Text to store string values.
Properties.Settings.Default.Setting1 = textBox2.Text;
Properties.Settings.Default.Save();
It is a working method for my all previous application.But I can't understand why its not working in my current project.
Since your question does not include many details, I've tried to answer as much as possible:
Try to perform a Properties.Settings.Default.Upgrade() and then saved settings get loaded.
You have to call the Upgrade method of ApplicationSettingsBase derived class (that is normally called Settings and is created for you by Visual Studio)
Properties.Settings.Default.Upgrade();
When/where to call the Upgrade method? There is a simple trick you can apply: define a user setting called UpgradeRequired (example) as bool (the easiest way is through IDE). Make sure its default value is true.
Insert this code snipped at the start of the application:
if (Properties.Settings.Default.UpgradeRequired)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpgradeRequired = false;
Properties.Settings.Default.Save();
}
So the Upgrade method will be called only after the version changes and only one time (since you disable further upgrades by setting UpgradeRequired = false until a version change - when the property regains default value of true).
Check the scope of your settings [USER/APPLICATION]
Try this out, and if it doesn't work check the below conditions.
Also, a more detailed question next time would be much appreciated.
Permission (NTFS permission)
Or Active directory permission
Or capacity of windows drive is full.
Or there exist two or more user folders and you checked another.
For example: There are two or three folders, user.domain, user.workgroupname, user.
Despite all this, I suggest you to learn about System.Reflection and develop your personalized method to save settings, the option provided by the Visual Studio isn't very dependable.
Hope this was helpful.

Getting settings from an old version

I've got a Winforms app that has quite a few settings (.settings file). These are saved (as far as I can tell) in C:\Users\[User's username]\AppData\Local\[My program name]\[Build or something]\1.0.0.0\user.config but whenever I make a new build and a user runs that version, it makes a new [Build or something] folder and starts over with a "fresh set" of settings. What is the best practice for rolling over the settings from a previous version?
(Some settings I want to be "brand new" every time a new version is run and some settings I want to be copied from the last version)
I am using method described in this post (it says Clickonce but it is also applicable to other types of apps): https://blogs.msdn.microsoft.com/rprabhu/2005/06/29/client-settings-faq/
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.Default.CallUpgrade) {
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.CallUpgrade = false;
}
This will ensure that Upgrade() is called only the first time the application runs after a new version is deployed.

Clickonce application not seeting AppSettings

I have a Full trust, online only, click-once application that relies on settings in the App.config --> appSettings collection.
Once published, the first time the application is executed, the settings are read and everything works fine. If I close the application and run it again, the items that are in appSettings are no longer there and appSettings.Count is equal to 0.
I have set the app.config as "Content" and to "Copy Always".
Any Idea what could be causing the items in appSettings to disappear? or a way to get around this?
Also, please note that the settings are only being read, and being used to determine how the application is run. We're not trying to manipulate the settings.
I guess you are using the settings wrong.
First of all, you need to mind the difference between application and user settings. Application settings are constant and can not be changed from your code (for example default values, connection strings, etc.). User settings are for settings that change when the application runs, usually because there is some sort of Settings dialog in your application.
Secondly, you need to access them properly. People tend to (and I don't know why) use things like ConfigurationManager or other stuff to access settings. It is far easier and less error-prone to use Properties.Settings.Default.SettingName for both application and user settings.
Changing a user setting would read like this:
Properties.Settings.Default.SettingName = settingValue;
Properties.Settings.Default.Save();
The call to Save is important, as otherwise the change will not be persisted.
Thirdly: No need to change the way app.config is included in your project. By default it will be renamed to applicationname.exe.config and will always be included in your output unless you say otherwise. The same goes for the ClickOnce installation: It will be included by default. You may actually break things if you play with this or the ClickOnce deployment settings for app.config! Revert them to default and leave them alone.
Now one case where the settings will be lost and reverted to defaults is when you update your application. In this case you will have to migrate your settings from the previously installed version, otherwise the user settings will be overridden with the default values from the installation. What you do to migrate the settings is:
Define a user setting named SettingsUpgradeRequired (name doesn't really matter - should be self-explaining, however) with a default value of true in the settings designer. Then, when your application starts, you do this:
if (Properties.Settings.Default.SettingsUpgradeRequired)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.SettingsUpgradeRequired = false;
Properties.Settings.Default.Save();
}
This makes sure a migration of the settings only occurs when required. SettingsUpgradeRequired will only be true upon the first installation (in which case Upgrade does nothing) and after an update of your application, because - as I said - by default the configuration from the ClickOnce installation will override the previous configuration until you perform the upgrade.

c# application setting not saved

Using VS2010 Express, Win7-64.
In my application I Have several Application settings.
Most (mixed types) are User Settings, one is Application Setting (a DataSet).
I use the built-in Settings table where the one setting scope is set as Application, while the others scope is User.
The User settings are all saved and retrieved OK, and can also be seen in the XML file.
(string userConfig = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.PerUserRoamingAndLocal).FilePath;)
Problem: While the User settings are saved OK, the one Application Setting is not saved and is not present in the relevant config file.
(string appConfig = ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None).FilePath;).
In the Settings.Design file we can see the Application Setting as read-only item (get only), which is OK, but no data is saved, and no data is found, (except a null DataSet).
There is similar DataSet in the User settings that is saved OK, and can be loaded.
To save the settings, I run:
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.Save();
Thanks
Application-scope settings are read only, and can only be changed at design time.
so when you call save method it does not persist the data.
User-scope settings, however, can be written at run time, just as you would change any property value.
for more information you can see Using Settings in C#.

C# Settings file - how to load?

I have an application where I need to store 3 set of string collections (user bookmarks and some other stuff). I have added a "Setting File" to my C# project, created the 3 settings variables of type Specialized String Collection. Everything appears to be fine, as I can add strings to each of the collections and invoke the .Save() method of Settings.Default. I can see my pretty string collections within the generated "user.config" file.
HOWEVER, when do these values get loaded? There is no .Load() method of Settings. Attempting to access the Settings.-setting variable- name throws a null exception error, so obviously these values are not loaded at launch time on their own.
What am I missing?
Perhaps you're having problems with differing versions of the application and settings file? Try calling ApplicationSettingsBase.Upgrade to migrate previous settings to a new version.
Note that you should only be calling Upgrade once, and only when you've actually updated your settings. Create a settings property called NeedsUpgrade with a default value of true, and execute the following when you application loads:
if (Settings.Default.NeedsUpgrade)
{
Settings.Default.Upgrade();
Settings.Default.NeedsUpgrade = false;
Settings.Default.Save();
}
With this, Upgrade will only be called when a new version of a settings file is created.

Categories