How to keep user settings on uninstall - c#

I'm using .NET user settings feature and I'm facing a problem.
When the application is uninstalled, then installed back, the user settings are lost.
I understand it's by design, and I want to be able to give the choice to the user in the installer.
Could you please give me some pointers to articles or documentation that will helps me?
Thanks a lot

.NET User Settings are not removed on uninstall. In fact the settings of all previous versions of the software are preserved in Local Settings directory.
When the new version is installed, a new version of the settings is created and default settings are used.
To ensure your application will merge new settings with previous configuration, you have to call Settings.Default.Upgrade() method.
So the solution is to manually remove settings on uninstall if we don't want to preserve them. Since what I needed was preserving previous settings, all I do now is creating a new setting called UpgradeRequired with true has the default value, then add this code at application startup:
if (Properties.Settings.Default.UpdateRequired)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpdateRequired = false;
}

You could possibly write the settings you wish to save out to the registry or write them as an XML file to a location that won't be impacted by the uninstall.

If you want to keep using User Settings i would suggest writing a custom installer class, and implement the onUninstalling method, to go find the file and copy it to another location known to the onInstall method of your custom installer. So that the next time the installer runs it could find the file.

I don't think you want to actually persist data on the users machine after an uninstall. Leaving files around is an evil practice, a big no-no. You should expose a feature in the application itself to either export those settings to a location of their choice and to then import it again after re-installing the app or to synchronize those settings onto a server so they're automatically available upon re-install, etc. On an uninstall you should leave no trace behind.

Related

Update user settings from outside the application

I have a C# application (a VSTO office addin) running on clients' machines, and I need to make a setup project or any other EXE that can overwrite and update the user settings in the user.config files with my own values.
It seems that I can't know exactly where the user.config is- it seems to create different folders under AppData\Local\Microsoft_Corporation with different names and numbers. I can try to iterate every subfolder I find but I was hoping for something better.
I can't get the path by using ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal) because I'm not inside the application, and I tried it inside the application and it actually returned a different, wrong path.
I can overwrite the app.config because I know where the application sits, from the registry, but overwriting it doesn't change user settings if the user settings were already saved in user.config too.
Can I force .NET to write the user.config into a predetermined location and store it in the registry, so I would have it for my setup?
Any ideas how to complete this objective? Is there some common practice for this? Thank you
Don't update the settings from your installer. The installer can save new options locally (HKLM, local folder, etc.) visible to every local user. When your addin is loaded, it can grab the new settings from that location and update its local settings as necessary.

Force all user settings to save

I have a C# program with various setting stored in the program project settings. As it is a command line program, these settings will be changed by the user in the user.config file. I can't work out how to force it to save all the default values.
Calling Properties.Settings.Default.Save(); only seems to work when I update a property in the program, which defeats the point of a persistent settings file.
If I run
Properties.Settings.Default.SomeSetting = "Help";
Properties.Settings.Default.Save();
In the config file I will see something like
< setting name="SomeSetting" serializeAs="String">
<value>Help</value>
< /setting>
What I want to happens is if the config file doesn't exist, create it and store all the settings. Then the user can change them if needed.
The way I managed to solve it was by modifying some existing code I had in a different project to handle version upgrades.
When the program runs:
if (PropertiesSettings.Default.UpgradeRequired)
{
Properties.Settings.Default.Upgrade();
Properties.Settings.Default.UpgradeRequired = false;
Properties.Settings.Default.Save();
reloadSettings();
}
// Assign settings to themselves and save
// This keeps settings the same as the defaults even when upgrading but ensures all settings appear in the config file
private void reloadSettings()
{
Properties.Settings.Default.Setting1 = Properties.Settings.Default.Setting1;
Properties.Settings.Default.Setting2 = Properties.Settings.Default.Setting2;
//etc
Properties.Settings.Default.Save();
}
The UpgradeRequired setting should be set to True by default, so a new version with no config will be forced to run the first if statement. The .NET Properties.Settings.Default.Upgrade(); should then take care of finding old config files and loading the old settings.
Settings are saved somewhere in user AppData folder specifically for a given process. For example, when the application is run under the debugger, you get different settings than when running stand-alone.
I would guess that there is no easy way to share settings across application using those properties settings. That code is quite old (.NET 2.0 or even before) so I think it is not designed to be easily customizable as it is often the case with more recent code.
I think that settings are good for things that are specific to a given application and if data need to be shared across application, then you should manage your own files.
Usually, one would use either XML or JSON based file and the data would be stored (by default) either under a subdirectory of My Documents if the data is intended to be visible to users or under a subdirectory of user application data directory if a regular user should not see those files.

Get previous version settings for C# application

I have an old Winforms application that stores user settings. I have completely rewritten the application in WPF and would like to migrate the settings. I can't use the Upgrade() method since I have changed the names of most of the user settings. I found the GetPreviousVersion() method but it fails since the old settings no longer exist in the new application. Also, the applications are in different namespace's which I hear causes some issues. Any help would be great.
Why not open up the original application and run Upgrade() just to see the results it produces? You could then also try GetPreviousVersion() too.

VSTO Outlook addin need to save settings, best way?

I'm writing a VSTO Outlook add in and i need to save some settings the addin gets from a web service. What is the best way to do this. Registry? does the VSTO addin have full access to do something like that? Maybe a file containing the settings?
Thanks in advance.
You can use a Settings (.settings) file.
The advantage of this file, besides having a centralized and strongly-typed repository, is that you can make these settings either application-scoped or user-scoped. Application settings will be available to all users of the computer. User settings will be individualized for each user. (I believe the framework will actually store these settings in separate files somewhere in the OS. I'm not sure, but it doesn't matter. The beauty of the Settings file is that it takes care of the actual storage and retrieval for you.)
You can use a Settings file as per #Keith's answer.
There's some discussion in the comments of that answer saying that the settings will be lost if the Office version is upgraded, because the path to the settings file includes the Office version number.
While that's true, there's an easy solution - simply use settings.Upgrade.

C# application version management

I have a C# app with its corresponding setup program. It reads startup data from the registry and stores information under the "Environment.SpecialFolder.ApplicationData"\myapp folder.
My question is: If I make various changes to the code that include new registry keys, where should I reflect this in the new version so when re-running the setup it:
Keeps current registry keys/valuesAdds new Key/value to the registryLeaves "Application Data" untouched
The candidates are:
Assembly InfoFile Version
If there are others please let me know.
Thanks
I think your application should be able to create the regestry keys. You don't know if the user removes them manually to reset the settings to default. The Setup should only install and register components. (But the Uninstaller should ask the User if he want to remove the settings, or keep them.)
Solution A)
To avoid version-conflicts you can remember a extra version number for configuration. As an Example: Program Version 1.0 and 2.0 are using the configuration-set 1 while Program Version 3.0 uses a configuration-set 2, because there are obsolete/new settings. Just create a sub-key in your registry tree for every configuration-set.
"Application Data" can also include a subfolder with seperated configuration sets.
The major advantage is, that the user can downgrade without problems.
New versions can import old values from older configuration as default values.
Solution B)
Never delete (or change the type or range) settings. Just add new keys. Old program versions don't know them so they just ignoring it. This is very simple because there are no different configuration versions. But if your saving a lot of settings it can become complicated, because you cannot change the type of a setting. As an example you have to create a new key to change a setting from saving an integer to a long integer, because old versions don't understand long integers and will crash.

Categories

Resources