/// <summary> /// Clones a file or a directory tree into a new path. /// All files in the target will be erased. /// </summary> public static void Clone(string sourcePath, string targetPath) { if (FilesystemTools.IsDirectory(sourcePath)) { if (FilesystemTools.Exists(targetPath)) { FilesystemTools.SafeDelete(targetPath, true, true); } Directory.CreateDirectory(targetPath); // Create all of the directories foreach (string dirPath in Directory.GetDirectories(sourcePath, "*", SearchOption.AllDirectories)) { Directory.CreateDirectory(dirPath.Replace(sourcePath, targetPath)); } //Copy all the files & Replaces any files with the same name foreach (string newPath in Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories)) { File.Copy(newPath, newPath.Replace(sourcePath, targetPath), true); } } else { File.Copy(sourcePath, targetPath, true); } }
public void SafeDelete_should_delete_readonly_files_when_specified() { var file = CreateFile(@"testFile", true); Executing.This(() => FilesystemTools.SafeDelete(file)).Should().Throw(); Executing.This(() => FilesystemTools.SafeDelete(file, false, true)).Should().NotThrow(); File.Exists(file).Should().Be.False(); }
public void SafeDelete_should_delete_directories_with_readonly_files() { var dir = CreateDir(@"\TestDir"); CreateDir(@"\TestDir\SubDir"); CreateFile(@"\TestDir\SubDir\testFile", true); Executing.This(() => FilesystemTools.SafeDelete(dir, true)).Should().Throw(); FilesystemTools.SafeDelete(dir, true, true); Directory.Exists(dir).Should().Be.False(); }
public void SafeDelete_should_delete_directories_recursively_when_specified() { var dir = CreateDir(@"\TestDir"); CreateDir(@"\TestDir\SubDir"); CreateFile(@"\TestDir\SubDir\testFile"); Executing.This(() => FilesystemTools.SafeDelete(dir)).Should().Throw(); FilesystemTools.SafeDelete(dir, true); Directory.Exists(dir).Should().Be.False(); }
public void SafeDelete_should_delete_files() { using (var tmp = new TempFile()) { tmp.Create(); FilesystemTools.SafeDelete(tmp.Path); File.Exists(tmp.Path).Should().Be.False(); } }
public void SafeDelete_should_delete_readonly_files_recursively() { var file = CreateFile(@"testFile"); var dir = CreateDir(@"TestDir"); CreateDir(@"TestDir\SubDir"); CreateFile(@"TestDir\SubDir\testFile"); FilesystemTools.SafeDelete(file); File.Exists(file).Should().Be.False(); Executing.This(() => FilesystemTools.SafeDelete(dir)).Should().Throw(); FilesystemTools.SafeDelete(dir, true); Directory.Exists(dir).Should().Be.False(); Executing.This(() => FilesystemTools.SafeDelete(Path.Combine(TestRoot, "nothing"))) .Should().NotThrow(); }