/// <summary> /// Copies a directory, expanding LZX compressed files if found. /// </summary> /// <param name="sourceDirectory">The source directory.</param> /// <param name="destinationDirectory">The destination directory, or null to put in an "Expanded" directory.</param> /// <param name="overwrite">True to overwrite files in the destination path.</param> /// <param name="throwOnBadCompression">If false and the compression is bad, source files will be copied normally.</param> public static void LzCopyDirectory( string sourceDirectory, string destinationDirectory = null, bool overwrite = false, bool throwOnBadCompression = false) { if (!FileMethods.DirectoryExists(sourceDirectory)) { throw new DirectoryNotFoundException(sourceDirectory); } if (destinationDirectory == null) { destinationDirectory = Paths.Combine(Paths.TrimLastSegment(sourceDirectory), "Expanded", Paths.GetLastSegment(sourceDirectory)); } foreach (var file in Directory.EnumerateFiles(sourceDirectory, "*", SearchOption.AllDirectories)) { string expandedName = GetExpandedNameEx(file, filenameOnly: true); string targetDirectory = Paths.TrimLastSegment(Paths.Combine(destinationDirectory, file.Substring(sourceDirectory.Length + 1))); Directory.CreateDirectory(targetDirectory); int result = LzCopyFile(file, Paths.Combine(targetDirectory, expandedName), overwrite: overwrite, throwOnBadCompression: throwOnBadCompression); if (result < 0) { // Bad source file perhaps, attempt a normal copy FileMethods.CopyFile(file, Paths.Combine(targetDirectory, Paths.GetLastSegment(file)), overwrite: overwrite); } } }
public void FileNotExistsTests() { using (var cleaner = new TestFileCleaner()) { string filePath = cleaner.GetTestPath(); FileMethods.FileExists(filePath).Should().BeFalse(); FileMethods.PathExists(filePath).Should().BeFalse(); FileMethods.DirectoryExists(filePath).Should().BeFalse(); } }
public void FileExistsTests() { using (var cleaner = new TestFileCleaner()) { string filePath = cleaner.GetTestPath(); FileHelper.WriteAllText(filePath, "FileExists"); FileMethods.FileExists(filePath).Should().BeTrue(); FileMethods.PathExists(filePath).Should().BeTrue(); FileMethods.DirectoryExists(filePath).Should().BeFalse(); } }
public void DirectoryExistsTests() { using (var cleaner = new TestFileCleaner()) { string directoryPath = cleaner.GetTestPath(); FileHelper.CreateDirectoryRecursive(directoryPath); FileMethods.FileExists(directoryPath).Should().BeFalse(); FileMethods.PathExists(directoryPath).Should().BeTrue(); FileMethods.DirectoryExists(directoryPath).Should().BeTrue(); } }
public void LongPathFileNotExistsTests() { using (var cleaner = new TestFileCleaner()) { string longPath = @"\\?\" + PathGenerator.CreatePathOfLength(cleaner.TempFolder, 500); string filePath = cleaner.GetTestPath(); FileMethods.FileExists(filePath).Should().BeFalse(); FileMethods.PathExists(filePath).Should().BeFalse(); FileMethods.DirectoryExists(filePath).Should().BeFalse(); } }
public void LockedFileDirectoryDeletion() { using (var cleaner = new TestFileCleaner()) { string directory = cleaner.GetTestPath(); DirectoryMethods.CreateDirectory(directory); FileMethods.DirectoryExists(directory).Should().BeTrue(); string file = cleaner.CreateTestFile(nameof(LockedFileDirectoryDeletion), directory); using (var handle = FileMethods.CreateFile(file, CreationDisposition.OpenExisting, DesiredAccess.GenericRead, ShareModes.ReadWrite | ShareModes.Delete)) { handle.IsInvalid.Should().BeFalse(); // Mark the file for deletion FileMethods.DeleteFile(file); // RemoveDirectory API call will throw Action action = () => DirectoryMethods.RemoveDirectory(directory); action.ShouldThrow <WInteropIOException>().And.HResult.Should().Be((int)ErrorMacros.HRESULT_FROM_WIN32(WindowsError.ERROR_DIR_NOT_EMPTY)); // Opening the directory for deletion will succeed, but have no impact using (var directoryHandle = FileMethods.CreateFile( directory, CreationDisposition.OpenExisting, DesiredAccess.ListDirectory | DesiredAccess.Delete, ShareModes.ReadWrite | ShareModes.Delete, FileAttributes.None, FileFlags.BackupSemantics | FileFlags.DeleteOnClose)) { directoryHandle.IsInvalid.Should().BeFalse(); } } // File will be gone now that the handle is closed FileMethods.FileExists(file).Should().BeFalse(); // But the directory will still exist as it doesn't respect DeleteOnClose with an open handle when it is closed FileMethods.DirectoryExists(directory).Should().BeTrue(); // Create a handle to the directory again with DeleteOnClose and it will actually delete the directory using (var directoryHandle = FileMethods.CreateFile( directory, CreationDisposition.OpenExisting, DesiredAccess.ListDirectory | DesiredAccess.Delete, ShareModes.ReadWrite | ShareModes.Delete, FileAttributes.None, FileFlags.BackupSemantics | FileFlags.DeleteOnClose)) { directoryHandle.IsInvalid.Should().BeFalse(); } FileMethods.DirectoryExists(directory).Should().BeFalse(); } }
public void LongPathFileExistsTests() { using (var cleaner = new TestFileCleaner()) { string longPath = @"\\?\" + PathGenerator.CreatePathOfLength(cleaner.TempFolder, 500); FileHelper.CreateDirectoryRecursive(longPath); string filePath = cleaner.CreateTestFile("FileExists", longPath); FileMethods.FileExists(filePath).Should().BeTrue(); FileMethods.PathExists(filePath).Should().BeTrue(); FileMethods.DirectoryExists(filePath).Should().BeFalse(); } }
public void CreateDirectoryDirect() { using (var cleaner = new TestFileCleaner()) { using (var directory = DirectoryMethods.CreateDirectoryHandle(cleaner.TempFolder)) { directory.IsInvalid.Should().BeFalse(); string name = System.IO.Path.GetRandomFileName(); using (var subdir = DirectoryMethods.CreateDirectory(directory, name)) { subdir.IsInvalid.Should().BeFalse(); FileMethods.DirectoryExists(Paths.Combine(cleaner.TempFolder, name)).Should().BeTrue(); } } } }
public void LockedFileDirectoryDeletion2() { using (var cleaner = new TestFileCleaner()) { string directory = cleaner.GetTestPath(); DirectoryMethods.CreateDirectory(directory); FileMethods.DirectoryExists(directory).Should().BeTrue(); string file = cleaner.CreateTestFile(nameof(LockedFileDirectoryDeletion2), directory); SafeFileHandle directoryHandle = null; using (var handle = FileMethods.CreateFile(file, CreationDisposition.OpenExisting, DesiredAccess.GenericRead, ShareModes.ReadWrite | ShareModes.Delete)) { handle.IsInvalid.Should().BeFalse(); // Mark the file for deletion FileMethods.DeleteFile(file); // Open the directory handle directoryHandle = FileMethods.CreateFile( directory, CreationDisposition.OpenExisting, DesiredAccess.ListDirectory | DesiredAccess.Delete, ShareModes.ReadWrite | ShareModes.Delete, FileAttributes.None, FileFlags.BackupSemantics | FileFlags.DeleteOnClose); } try { // File will be gone now that the handle is closed FileMethods.FileExists(file).Should().BeFalse(); directoryHandle.Close(); // The directory will not exist as the open handle was closed before it was closed FileMethods.DirectoryExists(directory).Should().BeFalse(); } finally { directoryHandle?.Close(); } } }