Archive

Posts Tagged ‘Isolated Storage’

IsolatedStorageSettings for WPF

System.IO.IsolatedStorageSettings doesn’t exist in WPF (only in Silverlight) but can easily be ported from Mono project’s Moonlight implementation (the Moonlight project home is at http://www.mono-project.com/Moonlight), as suggested at: http://groups.google.com/group/wpf-disciples/browse_thread/thread/4ed6c009f3fb7d69

I found that implementation at the following URL:

http://vega.frugalware.org/tmpgit/moon/class/System.Windows/System.IO.IsolatedStorage/IsolatedStorageSettings.cs

Did some modifications to IsolatedStorageSettings.cs to make it work with WPF (whether the application is 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
} return site_settings; } }

 

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

#if !SILVERLIGHT

Also can take a look at the following for custom settings storage:

http://f10andf11.blogspot.gr/2012/03/wpf-implement-isolatedstoragesettings.html

The resulting IsolatedStorageSettings.cs file for WPF (thanks to if !SILVERLIGHT block, the C# compiler will ignore it in Silverlight which has such class already) is included in the WPF_Compatibility layer at the ClipFlair source-code in http://clipflair.codeplex.com (checkout the “Client” subfolder in the source).

The WPF compatibility layer contains other goodies too like value coercion for Silverlight DependencyProperties using WPF-compatible syntax, so that your source code can stay source-level compatible with both WPF and Silverlight and be shared among respective projects.

%d bloggers like this: