public static void Save <T>(T o, string filenameONLY = null, IsolatedStorageScope iss = IsoConst.PdFls) { try { var isoStore = IsolatedStorageFile.GetStore(iss, null, null); using (var isoStream = new IsolatedStorageFileStream(IsoHelper.GetSetFilename <T>(filenameONLY, "json"), FileMode.Create, isoStore)) { using (var streamWriter = new StreamWriter(isoStream)) { new DataContractJsonSerializer(typeof(T)).WriteObject(streamWriter.BaseStream, o); // new XmlSerializer(o.GetType()).Serialize(streamWriter, o); streamWriter.Close(); } } } catch (Exception ex) { ex.Log(); throw; } }
public static T Load <T>(string filenameONLY = null, IsolatedStorageScope iss = IsoConst.PdFls) { try { var isoStore = IsolatedStorageFile.GetStore(iss, null, null); if (isoStore.FileExists(IsoHelper.GetSetFilename <T>(filenameONLY, "json"))) { using (var isoStream = new IsolatedStorageFileStream(IsoHelper.GetSetFilename <T>(filenameONLY, "json"), FileMode.Open, FileAccess.Read, FileShare.Read, isoStore)) { using (var streamReader = new StreamReader(isoStream)) { var o = (T)(new DataContractJsonSerializer(typeof(T)).ReadObject(streamReader.BaseStream)); // var o = (T)(new XmlSerializer(typeof(T)).Deserialize(streamReader)); streamReader.Close(); return(o); } } } } catch (InvalidOperationException /**/ ex) { if (ex.HResult != -2146233079) { ex.Log(); throw; } } // "Root element is missing." ==> create new at the bottom catch (SerializationException /**/ ex) { if (ex.HResult != -2146233076) { ex.Log(); throw; } } // "There was an error deserializing the object of type AAV.SS.Logic.AppSettings. End element 'LastSave' from namespace '' expected. Found element 'DateTime' from namespace ''." string catch (Exception /**/ ex) { ex.Log(); throw; } return((T)Activator.CreateInstance(typeof(T))); } } public static class XmlIsoFileSerializer { public static void Save <T>(T o, string filenameONLY = null, IsolatedStorageScope iss = IsoConst.PdFls) { try { var isoStore = IsolatedStorageFile.GetStore(iss, null, null); using (var isoStream = new IsolatedStorageFileStream(IsoHelper.GetSetFilename <T>(filenameONLY, "xml"), FileMode.Create, isoStore)) { IsoHelper.DevDbgLookup(isoStore, isoStream); using (var streamWriter = new StreamWriter(isoStream)) { new XmlSerializer(o.GetType()).Serialize(streamWriter, o); streamWriter.Close(); } } } catch (Exception ex) { ex.Log(); throw; } } public static T Load <T>(string filenameONLY = null, IsolatedStorageScope iss = IsoConst.PdFls) { try { var isoStore = IsolatedStorageFile.GetStore(iss, null, null); if (isoStore.FileExists(IsoHelper.GetSetFilename <T>(filenameONLY, "xml"))) { using (var stream = new IsolatedStorageFileStream(IsoHelper.GetSetFilename <T>(filenameONLY, "xml"), FileMode.Open, FileAccess.Read, FileShare.Read, isoStore)) { //Trace.WriteLine("ISO:/> " + stream.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(stream).ToString()); //Retrieve the actual path of the file using reflection. IsoHelper.DevDbgLookup(isoStore, stream); using (var streamReader = new StreamReader(stream)) { var o = (T)(new XmlSerializer(typeof(T)).Deserialize(streamReader)); streamReader.Close(); return(o); } } } } catch (InvalidOperationException ex) { if (ex.HResult != -2146233079) { ex.Log(); } throw; } // "Root element is missing." ==> create new at the bottom catch (Exception ex) { ex.Log(); throw; } return((T)Activator.CreateInstance(typeof(T))); } } public static class JsonStringSerializer { public static string Save <T>(T o) { var serializer = new DataContractJsonSerializer(typeof(T)); using (var ms = new MemoryStream()) { serializer.WriteObject(ms, o); var jsonArray = ms.ToArray(); return(Encoding.UTF8.GetString(jsonArray, 0, jsonArray.Length)); } } public static T Load <T>(string str) //catch ... : lets the caller handle the failure: it knows better what to do { if (string.IsNullOrEmpty(str)) { return((T)Activator.CreateInstance(typeof(T))); } using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(str))) return((T) new DataContractJsonSerializer(typeof(T)).ReadObject(ms)); // if ....: return (T)new DataContractJsonSerializer(typeof(T)).ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(str))); } } public static class XmlStringSerializer { public static string Save <T>(T o) { var sb = new StringBuilder(); using (var sw = new StringWriter(sb)) { new XmlSerializer(typeof(T)).Serialize(sw, o); sw.Close(); } return(sb.ToString()); } public static T Load <T>(string str) => (T) new XmlSerializer(typeof(T)).Deserialize(new StringReader(str)); } public static class IsoHelper { public static string GetSetFilename <T>(string filenameONLY, string ext) { var s = string.IsNullOrEmpty(filenameONLY) ? $"{typeof(T).Name}.{ext}" : filenameONLY; //Trace.WriteLine($":iso:>>>\t\r\n{GetIsoFolder()}\t\r\n{s}"); return(s); } [Obsolete("AAV-> ..instead use:\r\n System.IO.IsolatedStorage.IsolatedStorageFile.GetStore(System.IO.IsolatedStorage.IsolatedStorageScope.User | System.IO.IsolatedStorage.IsolatedStorageScope.Assembly, null, null)", true)] public static IsolatedStorageFile GetIsolatedStorageFile() => IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);// #if DEBUG// return IsolatedStorageFile.GetUserStoreForAssembly(); //todo: GetUserStoreForApplication does not work// #else// if (AppDomain.CurrentDomain.ActivationContext == null)// return IsolatedStorageFile.GetMachineStoreForDomain(); // C:\ProgramData\IsolatedStorage\... http://stackoverflow.com/questions/72626/could-not-find-file-when-using-isolated-storage // //return IsolatedStorageFile.GetMachineStoreForAssembly(); // else// return IsolatedStorageFile.GetUserStoreForApplication(); // C:\Users\Alex\AppData\Local\Apps\... http://stackoverflow.com/questions/202013/clickonce-and-isolatedstorage/227218#227218// #endif public static void DevDbgLookup(IsolatedStorageFile isoStoreF, IsolatedStorageFileStream isoStream) { #if DEBUG_ //..Trace.WriteLine(isoStoreF.GetType().GetField("m_RootDir", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(isoStoreF), ":>iso.m_rootDir "); //instead of creating a temp file and get the location you can get the path from the store directly: //..Trace.WriteLine(isoStream.GetType().GetField("m_FullPath", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(isoStream), ":>iso.m_fullPath "); //Retrieve the actual path of the file using reflection. #endif } public static void ListIsoFolders() { try { #if DEBUG //..Trace.WriteLine(AppDomain.CurrentDomain.ActivationContext == null, "ActivationContext==null"); ////Unable to determine application identity of the caller. try { twl(IsolatedStorageFile.GetMachineStoreForApplication /**/ (), "\r\n"); } catch (Exception ex) { Trace.WriteLine(ex.Message); }