/// <summary> /// Persists the data back to the filesystem /// </summary> public virtual void Save() { lock (SyncRoot) { if (!this.IsEmpty) { string file = this.DirectWrite ? _file : Path.GetTempFileName(); var value = _value; using (var scope = new FileLockScope(file)) { try { FileMode mode = File.Exists(file) ? FileMode.Truncate : FileMode.OpenOrCreate; using (FileStream stream = File.Open(file, mode, FileAccess.Write, FileShare.ReadWrite)) { _serializer.Serialize((Stream)stream, value); } if (!this.DirectWrite) { File.Copy(file, _file, true); File.Delete(file); } } catch (Exception ex) { throw new Exception(string.Format("Fail to save persitable object to file {0}.", file), ex); } } } } }
/// <summary> /// Loads the data from the filesystem. For deserialization an XmlSeralizer is used. /// </summary> public virtual void Load() { lock (SyncRoot) { string file = this._file; using (var scope = new FileLockScope(file)) { try { if (System.IO.File.Exists(file)) { using (FileStream reader = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { _value = (T)_serializer.Deserialize(reader); } } AddFileNotifier(); } catch (Exception ex) { throw new Exception(string.Format("Fail to load persitable object from file {0}.", file), ex); } } } }
/// <summary> /// Loads the data from the file system. For deserialization an XmlSeralizer is used. /// </summary> public virtual void Load() { try { using (var scope = new FileLockScope(_file)) { if (File.Exists(_file)) { using (FileStream reader = File.Open(_file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)) { _value = (T)_serializer.Deserialize(reader); } } AddFileNotifier(); } } catch (Exception ex) { throw new Exception(string.Format(Messages.LoadFailed, _file), ex); } }
/// <summary> /// Deletes the data from the cache and filesystem /// </summary> /// <returns></returns> public virtual bool Delete() { bool success = false; using (var scope = new FileLockScope(this._file)) { if (File.Exists(this._file)) { for (int i = 1; i < 4; i++) { try { File.Delete(this._file); success = true; } catch { Thread.Sleep(50 * i); } } } } return(success); }
/// <summary> /// Deletes the data from the cache and file system /// </summary> /// <returns></returns> public virtual bool Delete() { if (File.Exists(_file)) { using (var scope = new FileLockScope(_file)) { // loop just in case of any file locks while deleting for (int i = 1; i < 4; i++) { try { File.Delete(_file); return(true); } catch { Thread.Sleep(50 * i); } } } } return(false); }