Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you keep user.config settings across different assembly versions in .net?

Basically the problem is that each time the assembly version changes (i.e. the user installs a new version of the application) all their settings are reset the the defaults (or more accurately a new user.config file is created in a folder with a different version number as the name)

How can I keep the same settings when upgrading versions, since using ini files or the registry seem to be discouraged?

When we used Clickonce it seemed to be able to handle this, so it seems like it should be able to be done, but I'm not sure how.

like image 692
Davy8 Avatar asked Feb 10 '09 21:02

Davy8


People also ask

Where are .NET application settings stored?

Settings. settings is located in the My Project folder for Visual Basic projects and in the Properties folder for Visual C# projects. The Project Designer then searches for other settings files in the project's root folder. Therefore, you should put your custom settings file there.

Where are user settings stored C#?

User settings are saved in a file within a subfolder of the user's local hidden application data folder.

How add config file in C#?

In Visual Studio, from the Project menu, choose Add New Item. The Add New Item dialog box opens. In the Add New Item dialog box, select Settings File, enter a name for the file, and click Add to add a new settings file to your solution. In Solution Explorer, drag the new Settings file into the Properties folder.

Where is app config file in Visual Studio 2022?

In Solution Explorer, right-click the project node, and then select Add > New Item. The Add New Item dialog box appears. Expand Installed > Visual C# Items. In the middle pane, select the Application Configuration File template.


2 Answers

ApplicationSettingsBase has a method called Upgrade which migrates all settings from the previous version.

In order to run the merge whenever you publish a new version of your application you can define a boolean flag in your settings file that defaults to true. Name it UpgradeRequired or something similar.

Then, at application start you check to see if the flag is set and if it is, call the Upgrade method, set the flag to false and save your configuration.

if (Settings.Default.UpgradeRequired) {     Settings.Default.Upgrade();     Settings.Default.UpgradeRequired = false;     Settings.Default.Save(); } 

Read more about the Upgrade method at MSDN. The GetPreviousVersion might also be worth a look if you need to do some custom merging.

like image 186
Markus Olsson Avatar answered Sep 19 '22 14:09

Markus Olsson


The next short solution works for me when we need to upgrade only once per version. It does not required additional settings like UpgradeRequired:

if (!ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.PerUserRoamingAndLocal).HasFile)     Settings.Default.Upgrade(); 
like image 42
Argimko Avatar answered Sep 18 '22 14:09

Argimko