/// <summary> /// Appends the specified string the file, creating the file if it doesn't already exist. /// </summary> /// <param name="path">The file to append the string to.</param> /// <param name="contents">The string to append to the file.</param> public void AppendAllText(string path, string contents) { var r = new RollbackFile(path); try { File.AppendAllText(path, contents); } catch (Exception e) { r.CleanUp(); throw new Exception(e.Message, e); } if (_tx != null) { _journal.Add(r); Enlist(); } }
/// <summary> /// Copies the specified <paramref name="sourceFileName"/> to <paramref name="destFileName"/>. /// </summary> /// <param name="sourceFileName">The file to copy.</param> /// <param name="destFileName">The name of the destination file.</param> /// <param name="overwrite">true if the destination file can be overwritten, otherwise false.</param> public void Copy(string sourceFileName, string destFileName, bool overwrite) { var r = new RollbackFile(destFileName); try { File.Copy(sourceFileName, destFileName, overwrite); } catch (Exception e) { r.CleanUp(); throw new Exception(e.Message, e); } if (_tx != null) { _journal.Add(r); Enlist(); } }
/// <summary> /// Creates a file, and writes the specified <paramref name="contents"/> to the file. If the file /// already exists, it is overwritten. /// </summary> /// <param name="path">The file to write to.</param> /// <param name="contents">The bytes to write to the file.</param> public void WriteAllBytes(string path, byte[] contents) { var r = new RollbackFile(path); try { File.WriteAllBytes(path, contents); } catch (Exception e) { r.CleanUp(); throw new Exception(e.Message, e); } if (_tx != null) { _journal.Add(r); Enlist(); } }
/// <summary> /// Deletes the specified file or directory. An exception is not thrown if the file does not exist. /// </summary> /// <param name="path">The file to be deleted.</param> public void Delete(string path) { var r = new RollbackFile(path); try { File.Delete(path); } catch (Exception e) { r.CleanUp(); throw new Exception(e.Message, e); } if (_tx != null) { _journal.Add(r); Enlist(); } }
/// <summary> /// Moves the specified file to a new location. /// </summary> /// <param name="srcFileName">The name of the file to move.</param> /// <param name="destFileName">The new path for the file.</param> public void Move(string srcFileName, string destFileName) { var r1 = new RollbackFile(srcFileName); var r2 = new RollbackFile(destFileName); try { File.Move(srcFileName, destFileName); } catch (Exception e) { r1.CleanUp(); r2.CleanUp(); throw new Exception(e.Message, e); } if (_tx != null) { _journal.Add(r1); _journal.Add(r2); Enlist(); } }
/// <summary> /// Creates a file, and writes the specified <paramref name="stream"/> to the file. /// If the file already exists, it is overwritten. /// </summary> /// <param name="path">The file to write to.</param> /// <param name="stream">The stream to write to the file, which is disposed after the write.</param> public void WriteFileStream(string path, FileStream stream) { var r = new RollbackFile(path); try { using (var file = File.Create(path)) { stream.CopyTo(file); stream.Dispose(); } } catch (Exception e) { r.CleanUp(); throw new Exception(e.Message, e); } if (_tx != null) { _journal.Add(r); Enlist(); } }