Save key / value pairs to disk using WPF

I have a bunch of key / value pairs that I would like to cache for my WPF application. In Silverlight, this is very easy - I can just do:

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

Is there anything similar in WPF? Lombat cannot be marsupial, now I think about it. There was some work.

Edit: I would like, if I could, to avoid serializing them to / from en masse, since there will be a very large number of them with large amounts of data in them (I cache web pages).

+6
source share
3 answers

IsolatedStorageSettings does not exist in the desktop version of the .NET Framework, available only in Silverlight. However, you can use IsolatedStorage in any .NET application; just serialize Dictionary<string, object> into 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"]; 
+13
source

If by WPF you mean the full .Net runtime, then yes. There is a default settings class created using the WPF project template. class of settings

+6
source

See discussion

It does not exist in WPF, but it can be easily migrated from the Mono moonlight implementation (http://vega.frugalware.org/tmpgit/moon/class/System.Windows/System.IO.IsolatedStorage/IsolatedStorageSettings.cs)

  //Modifications at MoonLight 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 must also change the # if block at the top of this code to write

if! SILVERLIGHT

Also consider this for custom settings

+2
source

Source: https://habr.com/ru/post/888478/


All Articles