public void VerifyCommitAsTransaction() { using (TempFile temp1 = new TempFile()) using (TempFile temp2 = new TempFile()) { var options = new TransactedCompoundFile.Options(temp1.TempPath) { BlockSize = 512 }; const int count = 4; byte[] sample1 = new byte[options.MaxWriteSize / 3]; byte[] sample2 = (byte[])sample1.Clone(); new Random().NextBytes(sample1); new Random().NextBytes(sample2); using (TransactedCompoundFile file = new TransactedCompoundFile(options)) { for (uint h = 1u; h < count; h++) { Assert.AreEqual(h, file.Create()); } for (uint h = 1u; h < count; h++) { file.Write(h, sample1, 0, sample1.Length); } file.Commit();//persist. for (uint h = 1u; h < count; h++) { file.Write(h, sample2, 0, sample2.Length); } file.Commit(x => temp1.CopyTo(temp2.TempPath, true), 0); } options = new TransactedCompoundFile.Options(temp2.TempPath) { BlockSize = 512, CommitOnDispose = false }; //Verify primary has sample2 data options.LoadingRule = TransactedCompoundFile.LoadingRule.Primary; using (TransactedCompoundFile file = new TransactedCompoundFile(options)) { for (uint h = 1u; h < count; h++) { CompareBytes(sample2, 0, sample2.Length, IOStream.ReadAllBytes(file.Read(h))); } } //Verify secondary has sample1 data options.LoadingRule = TransactedCompoundFile.LoadingRule.Secondary; using (TransactedCompoundFile file = new TransactedCompoundFile(options)) { for (uint h = 1u; h < count; h++) { CompareBytes(sample1, 0, sample1.Length, IOStream.ReadAllBytes(file.Read(h))); } } } }
/// <summary> /// Copies a directory to another location /// </summary> /// <param name="Source">Source directory</param> /// <param name="Destination">Destination directory</param> /// <param name="Recursive">Should the copy be recursive</param> /// <param name="Options">Options used in copying</param> /// <returns>The DirectoryInfo for the destination info</returns> public static DirectoryInfo CopyTo(this DirectoryInfo Source, string Destination, bool Recursive = true, CopyOptions Options = CopyOptions.CopyAlways) { if (Source == null) { throw new ArgumentNullException("Source"); } if (!Source.Exists) { throw new DirectoryNotFoundException("Source directory " + Source.FullName + " not found."); } if (string.IsNullOrEmpty(Destination)) { throw new ArgumentNullException("Destination"); } DirectoryInfo DestinationInfo = new DirectoryInfo(Destination); DestinationInfo.Create(); foreach (FileInfo TempFile in Source.EnumerateFiles()) { if (Options == CopyOptions.CopyAlways) { TempFile.CopyTo(Path.Combine(DestinationInfo.FullName, TempFile.Name), true); } else if (Options == CopyOptions.CopyIfNewer) { if (File.Exists(Path.Combine(DestinationInfo.FullName, TempFile.Name))) { FileInfo FileInfo = new FileInfo(Path.Combine(DestinationInfo.FullName, TempFile.Name)); if (FileInfo.LastWriteTime.CompareTo(TempFile.LastWriteTime) < 0) { TempFile.CopyTo(Path.Combine(DestinationInfo.FullName, TempFile.Name), true); } } else { TempFile.CopyTo(Path.Combine(DestinationInfo.FullName, TempFile.Name), true); } } else if (Options == CopyOptions.DoNotOverwrite) { TempFile.CopyTo(Path.Combine(DestinationInfo.FullName, TempFile.Name), false); } } if (Recursive) { foreach (DirectoryInfo SubDirectory in Source.EnumerateDirectories()) { SubDirectory.CopyTo(Path.Combine(DestinationInfo.FullName, SubDirectory.Name), Recursive, Options); } } return(new DirectoryInfo(Destination)); }
/// <summary> /// Copies the directory to the specified parent directory /// </summary> /// <param name="Directory">Directory to copy to</param> /// <param name="Options">Copy options</param> /// <returns>Returns the new directory</returns> public virtual IDirectory CopyTo(IDirectory Directory, CopyOptions Options = CopyOptions.CopyAlways) { if (InternalDirectory == null || Directory == null) { return(this); } Directory.Create(); foreach (IFile TempFile in EnumerateFiles()) { switch (Options) { case CopyOptions.CopyAlways: TempFile.CopyTo(Directory, true); break; case CopyOptions.CopyIfNewer: if (new FileInfo(Directory.FullName + "\\" + TempFile.Name.Replace("/", "").Replace("\\", ""), UserName, Password, Domain).Exists) { var FileInfo = new FileInfo(Directory.FullName + "\\" + TempFile.Name.Replace("/", "").Replace("\\", ""), UserName, Password, Domain); if (FileInfo.Modified.CompareTo(TempFile.Modified) < 0) { TempFile.CopyTo(Directory, true); } } else { TempFile.CopyTo(Directory, true); } break; case CopyOptions.DoNotOverwrite: TempFile.CopyTo(Directory, false); break; } } foreach (IDirectory SubDirectory in EnumerateDirectories()) { SubDirectory.CopyTo(new DirectoryInfo(Directory.FullName + "\\" + SubDirectory.Name.Replace("/", "").Replace("\\", ""), UserName, Password, Domain), Options); } return(Directory); }
/// <summary> /// Copies the directory to the specified parent directory /// </summary> /// <param name="directory">Directory to copy to</param> /// <param name="options">Copy options</param> /// <returns>Returns the new directory</returns> public virtual IDirectory CopyTo(IDirectory directory, CopyOptions options = CopyOptions.CopyAlways) { if (InternalDirectory is null || directory is null || string.IsNullOrEmpty(directory.FullName)) { return(this); } directory.Create(); foreach (var TempFile in EnumerateFiles()) { switch (options) { case CopyOptions.CopyAlways: TempFile.CopyTo(directory, true); break; case CopyOptions.CopyIfNewer: if (new FileInfo(directory.FullName + "\\" + TempFile.Name.Replace("/", "").Replace("\\", ""), Credentials).Exists) { var FileInfo = new FileInfo(directory.FullName + "\\" + TempFile.Name.Replace("/", "").Replace("\\", ""), Credentials); if (FileInfo.Modified.CompareTo(TempFile.Modified) < 0) { TempFile.CopyTo(directory, true); } } else { TempFile.CopyTo(directory, true); } break; case CopyOptions.DoNotOverwrite: TempFile.CopyTo(directory, false); break; } } foreach (var SubDirectory in EnumerateDirectories()) { SubDirectory.CopyTo(new DirectoryInfo(directory.FullName + "\\" + SubDirectory.Name.Replace("/", "").Replace("\\", ""), Credentials), options); } return(directory); }
/// <summary> /// Copies the directory to the specified parent directory /// </summary> /// <param name="Directory1">The directory1.</param> /// <param name="Directory2">The directory2.</param> private void CopyTo(DirectoryInfo Directory1, DirectoryInfo Directory2) { if (Directory1 == null || Directory2 == null) { return; } Directory2.Create(); foreach (FileInfo TempFile in Directory1.EnumerateFiles()) { try { TempFile.CopyTo(Path.Combine(Directory2.FullName, TempFile.Name), true); } catch { } } foreach (DirectoryInfo SubDirectory in Directory1.EnumerateDirectories()) { CopyTo(SubDirectory, new DirectoryInfo(Path.Combine(Directory2.FullName, SubDirectory.Name))); } }
public void TestCopyTo() { TempFile filea = new TempFile(); File.WriteAllText(filea.TempPath, "Test"); Assert.AreEqual("Test", File.ReadAllText(filea.TempPath)); TempFile fileb = new TempFile(); Assert.AreNotEqual(filea.TempPath, fileb.TempPath); filea.CopyTo(fileb.TempPath, true); Assert.AreEqual("Test", File.ReadAllText(fileb.TempPath)); File.Delete(filea.TempPath); Assert.IsFalse(File.Exists(filea.TempPath)); fileb.CopyTo(filea.TempPath); Assert.AreEqual("Test", File.ReadAllText(filea.TempPath)); filea.Dispose(); fileb.Dispose(); }