Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storing key/value pairs on disk using WPF

Tags:

c#

wpf

I have a bunch of key/value pairs I'd like to cache for my WPF application. In Silverlight this is deliciously easy - I can just do:

IsolatedStorageSettings userSettings = IsolatedStorageSettings.ApplicationSettings;
wombat = (string)userSettings["marsupial"];

Is there anything like this in WPF? A wombat may not be a marsupial, now I think about it. Some work needed there.

Edit: I would like if I can to avoid serialising these to/from en masse, as there are going to be a very large number of them with large amounts of data in them (I'm caching web pages).

like image 829
Chris Rae Avatar asked May 18 '11 22:05

Chris Rae


3 Answers

The IsolatedStorageSettings doesn't exist in the desktop version of the .NET Framework, it's only available in Silverlight. However you can use IsolatedStorage in any .NET application; just serialize a Dictionary<string, object> to a file in isolated storage.

var settings = new Dictionary<string, object>();
settings.Add("marsupial", wombat);

BinaryFormatter formatter = new BinaryFormatter();
var store = IsolatedStorageFile.GetUserStoreForAssembly();

// Save
using (var stream = store.OpenFile("settings.cfg", FileMode.OpenOrCreate, FileAccess.Write))
{
    formatter.Serialize(stream, settings);
}

// Load
using (var stream = store.OpenFile("settings.cfg", FileMode.OpenOrCreate, FileAccess.Read))
{
    settings = (Dictionary<string, object>)formatter.Deserialize(stream);
}

wombat = (string)settings["marsupial"];
like image 175
Thomas Levesque Avatar answered Oct 05 '22 05:10

Thomas Levesque


If by WPF, you mean the full .Net runtime, then yes. There's a default Settings class created with the WPF project template. Settings class

like image 37
Ritch Melton Avatar answered Oct 05 '22 05:10

Ritch Melton


See this discussion

It doesn't exist in WPF but can easily be ported from Mono's moonlight implementation (http://vega.frugalware.org/tmpgit/moon/class/System.Windows/System.IO.IsolatedStorage/IsolatedStorageSettings.cs)

    //Modifications at MoonLight's IsolatedStorageSettings.cs to make it work with WPF (whether deployed via ClickOnce or not):

// per application, per-computer, per-user
public static IsolatedStorageSettings ApplicationSettings {
  get {
    if (application_settings == null) {
      application_settings = new IsolatedStorageSettings (
        (System.Threading.Thread.GetDomain().ActivationContext!=null)?
          IsolatedStorageFile.GetUserStoreForApplication() : //for WPF, apps deployed via ClickOnce will have a non-null ActivationContext
          IsolatedStorageFile.GetUserStoreForAssembly());
    }
    return application_settings;
  }
}

// per domain, per-computer, per-user
public static IsolatedStorageSettings SiteSettings {
  get {
    if (site_settings == null) {
      site_settings = new IsolatedStorageSettings (
        (System.Threading.Thread.GetDomain().ActivationContext!=null)?
          IsolatedStorageFile.GetUserStoreForApplication() : //for WPF, apps deployed via ClickOnce will have a non-null ActivationContext
          IsolatedStorageFile.GetUserStoreForAssembly());
          //IsolatedStorageFile.GetUserStoreForSite() works only for Silverlight applications
    }
    return site_settings;
  }
}

Note that you should also change the #if block at the top of that code to write

if !SILVERLIGHT

Also take a look at this for custom settings storage

like image 29
George Birbilis Avatar answered Oct 05 '22 04:10

George Birbilis