Coolthing Of Theday

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Wednesday, 12 June 2013

Using .Net Settings? Hate it when you rev your app and the settings "go away"? Here's why and how to fix that (cough... "Settings.Upgrade()" cough)

Posted on 18:26 by Unknown

John's Workshop - Automatically upgrading user settings after an application version change

Suppose you’re building a .NET desktop or console application and are using the standard Settings file to keep track of any user-specific settings, as well as providing their default values. Then, suppose you upgrade your application version and notice that all of the user settings have been reset to their default values! Why does this happen and what can you do about it?

What Seems to Be the Problem?

...

Here’s the important part: the application version number is part of the path to the settings file containing the user modified values! So, if you upgrade the version number of the application to 2.0.0.0 and run it again, the application would no longer see the user-made changes from version 1.0.0.0. This is because the application can’t find a folder containing the exact version number and it falls back to the embedded default settings.

The Solution

Luckily, there is a rather simple solution to the whole problem: call the Settings.Upgrade() method. This will actually collect and merge all user-modified settings from previous versions of your application and store them in a new user.config file, mapped to the current application version. So, you could basically call Settings.Upgrade() every time your application starts, or you could use…

A Smarter Solution

Here is a neat little trick I’ve come up with, which will help you detect whether a settings upgrade is required. Let’s see the code snippet first, and then I’ll explain:

...

And there you go! Here’s how the whole thing works: The first time your application starts, the UpgradeRequired flag will be set. This means that the Upgrade() method will be called (which won’t really do anything at this point) and the flag will be cleared. If you start the application again, the flag will be cleared and no upgrade will be performed. When you do upgrade the application version, however, the default settings will be used – and the default value of the UpgradeRequired flag is again True. So, the settings file will be upgraded and the flag will be cleared again. That’s it!

image

ApplicationSettingsBase.Upgrade Method

"...

The Upgrade method performs two actions to assure smooth transition to a new version of an application:

  • It notifies all of the corresponding settings providers of the existence of the upgraded application through a call to their IApplicationSettingsProvider.Upgrade method, assuming they have implemented the IApplicationSettingsProvider interface. This action is not performed if the settings wrapper class is marked with NoSettingsVersionUpgradeAttribute.

  • It reloads the values for all of the application settings.

You can override the default behavior of Upgrade to implement custom upgrading or merging behavior. Use the GetPreviousVersion method to retrieve individual values for a setting for the previous version of the application. Examples of custom upgrade behavior include:

  • Using new policy defaults that override one or more of the previous user-specified values or previous defaults.

  • Special translation of old values to be compatible with newer ranges, a different settings property group, and so on.

..."

sigh... I hate my old brain. I've been using Settings for forever and never knew about this feature (which was added in .Net 2.0). Now I knew the why, that the app version was in the file path, but never realized there was an upgrade method.

Anyway, John provides a great post on the how and why of it and how to build an even better upgrade code snippet.

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in .Net, Development | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • Mr. 7,000! This is my 7,000th post...
    Before this post; After; 20 visits between taking these snaps? Oh wait, that's probably me searching for past related posts....
  • "Windows Server Essentials Media Pack" (DNLA Stream, HTML5 and Dashboard Media stuff)
    Microsoft Downloads - Windows Server Essentials Media Pack This pack enables the media streaming functionality for Windows Server 2012...
  • Rad Gate Post... Get your Red Gate Post here...
    simple talk - Melanie Townsend - Get a copy of the Red Gate Post We recently put together a newspaper of some of the best articles fr...
  • Windows Management Framework 4.0 (PowerShell 4, PowerShell ISE, Management OData, WMI, etc.) now available
    Keith Hill's Blog - PowerShell 4.0 Now Available You can get PowerShell 4.0 for down level operating systems now via the WMF 4.0 d...
  • Viasfora - Your new favorite Visual Studio Text/*ML Editing Extension?
    Winterdom - Introducing Viasfora A couple of days ago, I unveiled Viasfora , my latest attempt at building a decently packaged extensi...
  • "Windows Server [2012 R2]: The Best Infrastructure to Run Linux Workloads"
    In the Cloud - What’s New in 2012 R2: Enabling Open Source Software Part 4 of a 9-part series . ... There are a lot of great s...
  • [Hardware Review] Life with Haswell... Haswell/Harris Beach Intel SDS Ultrabook Review - Part 2
    "So Greg, how's life with Haswell been?" "Pretty Sweet! (Mostly)" I've been given an opportunity to review t...
  • Fuzzy Lookup Add-In for Excel (Insert lame "Fuzzy, wuzzy was an Excel..." snip here)
    Microsoft Downloads - Fuzzy Lookup Add-In for Excel The Fuzzy Lookup Add-In for Excel performs fuzzy matching of textual data in Exce...
  • Caliburn.Micro v1.5.0 released (CM gets Tasks, Async/Await and Share/Setting for RT... and bug fixes of course)
    Caliburn.Micro - Caliburn.Micro v1.5.0 "Release Notes This release fixes many bugs. It also adds support for Task and async/a...
  • Just about everything you ever wanted to know about SQL Server Date and Time Data Types...
    CodeProject - Date and Time Data Types and Functions - SQL Server (2000, 2005, 2008, 2008 R2, 2012) Introduction It would be bette...

Categories

  • .Net
  • 3DPrinting
  • AFeedYouShouldRead
  • Agile
  • ALM
  • Amazon
  • Amiga
  • Analytics
  • Android
  • ASP.NET
  • Azure
  • BigData
  • bing
  • Blogging
  • Book
  • BookReview
  • BUILD
  • C
  • C#
  • C++
  • Career
  • Cat
  • cheatsheet
  • ClickOnce
  • Cloud
  • ComputerHardware
  • css
  • Data
  • DBA
  • DependencyInjection
  • Deployment
  • Design
  • Development
  • devops
  • DVCS
  • ebook
  • EDD
  • Education
  • EnterpriseLibrary
  • EntityFramework
  • Exchange
  • Expression
  • gadget
  • Game
  • GIT
  • Google
  • Government
  • Hadoop
  • hardware
  • HardwareReview
  • HaswellReview
  • HTML5
  • Humor
  • IE
  • IEExtension
  • IfAllElseFails
  • IIS
  • ILMerge
  • Image
  • Infographic
  • interview
  • InversionOfControl
  • Java
  • Javascript
  • Kinect
  • LightSwitch
  • LINQ
  • Linux
  • LosAngeles
  • Lucene
  • Lync
  • MEF
  • Metro
  • MicrosoftOffice
  • MicrosoftOutlook
  • Mono
  • MVC
  • MVVM
  • NetMon
  • NLP
  • NoSQL
  • NuGet
  • OData
  • OneNote
  • OpenXML
  • Paint.Net
  • Personal
  • Photosynth
  • Physics
  • portable
  • Poster
  • PowerShell
  • Preparedness
  • Presentation
  • Prism
  • PrivateCloud
  • RegEx
  • RemoteDesktop
  • Reporting
  • RIAServices
  • Science
  • ScienceFiction
  • Scratch
  • Scrum
  • ServiceBus
  • SharePoint
  • Silverlight
  • SimiValley
  • SPA
  • Space
  • SQLServer
  • Storyboard
  • Surface
  • SVG
  • SystemAdministration
  • T4
  • TeamBuild
  • TeamFoundationServer
  • TechEd
  • Training
  • TypeScript
  • UnitTesting
  • UnityApplicationBlock
  • Utility
  • Veteran
  • VirtualMachine
  • Visio
  • VisualBasic
  • VisualStudio
  • WCF
  • Web X.X
  • Webcast
  • WebFeed
  • WebMatrix
  • Windows
  • Windows7
  • Windows8
  • Windows8.1
  • WindowsHomeServer
  • WindowsLiveWriter
  • WindowsPhone
  • WindowsServer
  • WinRT
  • WiX
  • WMI
  • WOPI
  • WPF
  • XAML
  • XBox360
  • XboxOne
  • zombie

Blog Archive

  • ▼  2013 (500)
    • ►  December (12)
    • ►  November (61)
    • ►  October (65)
    • ►  September (38)
    • ►  August (47)
    • ►  July (75)
    • ▼  June (39)
      • 300 for 320 - 320 Windows 8.1. app and code sample...
      • "Utilizing SysInternals Tools for Windows Client" ...
      • Google .Net API's go portable... The v1.4.0 Google...
      • Goodbye Google Reader... Need an alternate app? He...
      • Scott (not that Scott) helps make your Live dev ea...
      • SSDT (SQL Server Data Tools) June 2013 Released
      • RSAT (Remote Server Administration Tools) for Wind...
      • Getting a picture of the "Blue" Wave...
      • Need some help up the WPF learning curve? "WPF Suc...
      • A Sweet Summer Sysinternals Suite Refresh
      • Robin's "So you're in San Francisco for Build? Stu...
      • TypeScript Succinctly - Free [Name/email-ware] eBook
      • Friday Humor: Cell Reception - Can you hear me...o...
      • Mark TechEd's you... See all four of Mark Russinov...
      • Fighting bugs in your LOB apps? Hearing about cras...
      • Greg's //build bets - My total and complete WAG's ...
      • Excel as a digital art drawing platform? Yep! Beau...
      • Want the world's best Science Fiction And Fantasy ...
      • Eric triple downs on his Free Microsoft eBook List...
      • Log Parser Studio 2.0 now out (Log Parser GUI++)
      • //build Windows Store App available
      • Booting Minecraft (or maybe Assembling Minecraft) ...
      • POP goes the code review... Prince Of Persia Apple...
      • So you want to be a dev at Microsoft? Here's two v...
      • "The Essential HTML5 Mobile" DZone Refcardz Cheat ...
      • WPF TextBlock vs Label, what's the diff?
      • A quick look at the coming Los Angeles Light Rail ...
      • Dev:"But it's new and shinny! Let's upgrade!" DBA:...
      • "Microsoft SQL Server 2014 CTP1 Product Guide" - O...
      • Win8Templates now at 71 - 71 Windows 8 App Design ...
      • Getting into the flow, surfing restaurant inspecti...
      • Mash some web API's with your brain and Windows 8 ...
      • Using .Net Settings? Hate it when you rev your app...
      • Tiny Tiny RSS WAMP, MAMP or LAMP installers, VM or...
      • WintellectNOW FTW - Wintellect now providing on-de...
      • Windows 8 Mail Tip - Pin Email accounts so you see...
      • Gov Webicons - Your one shop, 41 Agency stop for U...
      • With yesterday's TechEd VS/TFS announcements, does...
      • TechEd NA 2013 Day 1 Announcement Round-up - VS 20...
    • ►  May (40)
    • ►  April (42)
    • ►  March (39)
    • ►  February (42)
Powered by Blogger.

About Me

Unknown
View my complete profile