开发者

Storing key/value pairs on disk using WPF

开发者 https://www.devze.com 2023-03-06 17:37 出处:网络
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:

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开发者_如何学JAVA 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).


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"];


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消