public static void PersistDeviceCodeHistory(DeviceCodeHistory codeHistory) { lock (codeHistory) { Stream stream = null; try { IFormatter formatter = new BinaryFormatter(); stream = new FileStream(codeHistory.Filename, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None); formatter.Serialize(stream, codeHistory); CNXLog.WarnFormat("Persisted DeviceCodeHistory {0}.", codeHistory.ToString()); } catch (Exception e) { CNXLog.Error("PersistDeviceCodeHistory", e); } finally { if (null != stream) { stream.Flush(); stream.Close(); } } } }
public static DeviceCodeHistory CreateDeviceCodeHistory(string filename) { DeviceCodeHistory dch = null; Stream stream = null; // load existing codes try { IFormatter formatter = new BinaryFormatter(); stream = new FileStream(filename, FileMode.Open, FileAccess.Read, FileShare.None); dch = (DeviceCodeHistory)formatter.Deserialize(stream); CNXLog.WarnFormat("CreateDeviceCodeHistory : from ({0}), {1}.", filename, dch.ToString()); } catch (FileNotFoundException) { CNXLog.WarnFormat("CreateDeviceCodeHistory : Could not open persistent storage ({0}), Creating a new one.", filename); } catch (Exception e) { CNXLog.Error("CreateDeviceCodeHistory", e); } finally { if (null != stream) { stream.Close(); } } // may have a failure or no persitant store if (dch == null) { dch = new DeviceCodeHistory(); } dch.Filename = filename; return(dch); }