Click here to Skip to main content
15,867,771 members
Articles / Programming Languages / C#
Tip/Trick

Upgrading Settings files when changing application version

Rate me:
Please Sign up or sign in to vote.
4.20/5 (3 votes)
8 Aug 2013CPOL 12.9K   6   5
If (like me) you use the Assembly version of your EXE to handle versioning, and you (like me) use the Settings.Settings for configuration items, then you've probably noticed the config is lost when the version is changed.

Introduction

When you change the Assembly version, the next load from the configuration data will come back with default values - because the version has been changed and .NET isn't sure the config will be valid. For users, this is either annoying, or harmful depending on what information you choose to store. This Tip provides a simple solution to keeping existing configuration.

Using the code

Add a new Setting to your assembly, call it ApplicationSettingsVersion, make it a string, leave it User, and leave the default value blank. 

Paste the code into your main Form constructor, above any configuration access. 

C#
// Prevent need to lose config settings on upgrade.
// Note: You cannot put this code into a utilities
// DLL - it gets the version of the Assembly that
// the code is running in - which is what we need
// as the utilities DLL could have it's own settings...
string thisVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
if (Properties.Settings.Default.ApplicationSettingsVersion != thisVersion)
    {
    Properties.Settings.Default.Upgrade();
    Properties.Settings.Default.ApplicationSettingsVersion = thisVersion;
    }

As the comments say: you can't move this to a utility assembly, and call the method, because you want to upgrade the settings for the main EXE file - and while you could use GetEntryAssembly instead of GetExecutingAssembly to get the correct  Assembly, it would affect the wrong settings, as the DLL could have it's own, separate settings file - mine do for common utility look-and-feel. 

You need to execute this code in each Assembly with it's own settings.  

History

Original Version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
GeneralMy vote of 3 Pin
satish koladiya26-Aug-13 23:37
professionalsatish koladiya26-Aug-13 23:37 
GeneralRe: My vote of 3 Pin
OriginalGriff27-Aug-13 0:31
mveOriginalGriff27-Aug-13 0:31 
GeneralRe: My vote of 3 Pin
Brisingr Aerowing20-Sep-13 5:41
professionalBrisingr Aerowing20-Sep-13 5:41 
GeneralRe: My vote of 3 Pin
OriginalGriff20-Sep-13 5:56
mveOriginalGriff20-Sep-13 5:56 
GeneralRe: My vote of 3 Pin
Brisingr Aerowing20-Sep-13 14:43
professionalBrisingr Aerowing20-Sep-13 14:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.