Saturday, June 25, 2011

Windows Phone 7 – Quick Tip #30 – Quickly Save Configuration Settings

Today’s tip is a real simple one, you need to save some sort of configuration setting to some persistent storage mechanism.  You can certainly write the values out to a file with IsolatedStorageFile.GetUserStoreForApplication.  But let’s just say you just want to save a string or a number, it might be worthwhile to build up some sort of config or settings class and serialize that with DataContractSerializer but it might be easier to just use IsolatedStorageSettings.ApplicationSettings

ApplicationSettings has some nice static methods to act as a Key-Value pair local storage mechanism.  It even has a TryGetValue method so you can easily build some intelligence into initializing your settings.

Using ApplicationSettings is as simple as:

IsolatedStorageSettings.ApplicationSettings[“KEY”] = value

and

value = IsolatedStorageSettings.ApplicationSettings[“KEY”]

Just make sure you remember to call the save method after setting a new value.

IsolatedStorageSettings.ApplicationSettings.Save();

No rocket-science today, but this might be exactly what you need to shed a few lines of code from your application.

-twb

No comments:

Post a Comment