Last active
July 25, 2016 09:30
-
-
Save arman-hpp/7b295a4c85faaadbd1094d1caadcef22 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class IsolatedStorageController | |
{ | |
private static readonly JsonSerializer Serializer; | |
private static readonly IsolatedStorageScope Scope; | |
private static readonly Type DomainEvidenceType; | |
private static readonly Type AssemblyEvidenceType; | |
static IsolatedStorageController() | |
{ | |
Serializer = new JsonSerializer(); | |
//Serializer.Converters.Add(new JavaScriptDateTimeConverter()); | |
//Serializer.NullValueHandling = NullValueHandling.Ignore; | |
Scope = IsolatedStorageScope.User | | |
IsolatedStorageScope.Assembly | | |
IsolatedStorageScope.Domain; | |
DomainEvidenceType = typeof (System.Security.Policy.Url); | |
AssemblyEvidenceType = typeof(System.Security.Policy.Url); | |
} | |
public static void AddOrUpdate(string fileName, object data) | |
{ | |
using (var store = IsolatedStorageFile.GetStore(Scope, DomainEvidenceType, AssemblyEvidenceType)) | |
{ | |
if (store.FileExists(fileName)) | |
store.DeleteFile(fileName); | |
using (var sw = new StreamWriter(store.CreateFile(fileName))) | |
using (JsonWriter jw = new JsonTextWriter(sw)) | |
{ | |
Serializer.Serialize(jw, data); | |
} | |
} | |
} | |
public static object Get(string fileName) | |
{ | |
using (var store = IsolatedStorageFile.GetStore(Scope, DomainEvidenceType, AssemblyEvidenceType)) | |
{ | |
if (!store.FileExists(fileName)) return null; | |
using (var sr = new StreamReader(store.OpenFile(fileName, FileMode.Open))) | |
using (var jr = new JsonTextReader(sr)) | |
{ | |
return Serializer.Deserialize(jr); | |
} | |
} | |
} | |
public static T Get<T>(string fileName) | |
{ | |
using (var store = IsolatedStorageFile.GetStore(Scope, DomainEvidenceType, AssemblyEvidenceType)) | |
{ | |
using (var sr = new StreamReader(store.OpenFile(fileName, FileMode.Open))) | |
using (var jr = new JsonTextReader(sr)) | |
{ | |
return Serializer.Deserialize<T>(jr); | |
} | |
} | |
} | |
public static void Remove(string fileName) | |
{ | |
using (var store = IsolatedStorageFile.GetStore(Scope, DomainEvidenceType, AssemblyEvidenceType)) | |
{ | |
if (!store.FileExists(fileName)) return; | |
store.DeleteFile(fileName); | |
} | |
} | |
public static void RemoveAll() | |
{ | |
using (var store = IsolatedStorageFile.GetStore(Scope, DomainEvidenceType, AssemblyEvidenceType)) | |
{ | |
store.Remove(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment