public void GetFileAttributesBehavior_DeletedFile() { using (var cleaner = new TestFileCleaner()) { string path = cleaner.CreateTestFile(nameof(GetFileAttributesBehavior_DeletedFile)); using (var handle = FileMethods.CreateFile(path, CreationDisposition.OpenExisting, shareMode: ShareModes.All)) { handle.IsInvalid.Should().BeFalse(); FileMethods.FileExists(path).Should().BeTrue(); FileMethods.DeleteFile(path); // With the file deleted and the handle still open the file will still physically exist. // Trying to access the file via a handle at this point will fail with access denied. Action action = () => FileMethods.FileExists(path); action.ShouldThrow <UnauthorizedAccessException>(); action = () => FileMethods.CreateFile(path, CreationDisposition.OpenExisting, shareMode: ShareModes.All, desiredAccess: DesiredAccess.ReadAttributes); action.ShouldThrow <UnauthorizedAccessException>(); // Find file will work at this point. IntPtr findHandle = FileMethods.Imports.FindFirstFileW(path, out WIN32_FIND_DATA findData); findHandle.Should().NotBe(IntPtr.Zero); try { findData.cFileName.CreateString().Should().Be(Paths.GetLastSegment(path)); } finally { FileMethods.Imports.FindClose(findHandle); } } } }
private string GetNativeTestLibraryLocation() { string path = Paths.Combine(System.IO.Path.GetDirectoryName((new Uri(typeof(Methods).Assembly.CodeBase)).LocalPath), NativeTestLibrary); if (!FileMethods.FileExists(path)) { throw new System.IO.FileNotFoundException(path); } return(path); }
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 CreateFileUnextendedTests(string fileName) { using (var cleaner = new TestFileCleaner()) { string filePath = Paths.Combine(cleaner.TempFolder, fileName); using (var handle = FileMethods.CreateFile(filePath, CreationDisposition.CreateNew)) { handle.IsInvalid.Should().BeFalse(); FileMethods.FileExists(filePath).Should().BeTrue(); } } }
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 CreateRelativeToDirectoryHandle() { using (var cleaner = new TestFileCleaner()) { string fileName = System.IO.Path.GetRandomFileName(); using (var directory = DirectoryMethods.CreateDirectoryHandle(cleaner.TempFolder)) { directory.IsInvalid.Should().BeFalse(); using (var file = FileMethods.CreateFileRelative(fileName, directory, CreateDisposition.Create)) { file.IsInvalid.Should().BeFalse(); } FileMethods.FileExists(Paths.Combine(cleaner.TempFolder, fileName)).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(); } } }
public void FinalPathNameForLinks() { if (!CanCreateSymbolicLinks()) { return; } // GetFinalPathName always points to the linked file unless you specifically open the reparse point using (var cleaner = new TestFileCleaner()) { string filePath = Paths.Combine(cleaner.TempFolder, "Target"); string extendedPath = @"\\?\" + filePath; FileHelper.WriteAllText(filePath, "CreateSymbolicLinkToFile"); string symbolicLink = Paths.Combine(cleaner.TempFolder, "Link"); string extendedLink = @"\\?\" + symbolicLink; FileDesktopMethods.CreateSymbolicLink(symbolicLink, filePath); FileMethods.FileExists(symbolicLink).Should().BeTrue("symbolic link should exist"); // GetFinalPathName should normalize the casing, pushing ToUpper to validate using (var handle = FileMethods.CreateFile(symbolicLink.ToUpperInvariant(), DesiredAccess.FILE_GENERIC_READ, ShareMode.FILE_SHARE_READWRITE, CreationDisposition.OPEN_EXISTING)) { handle.IsInvalid.Should().BeFalse(); FileDesktopMethods.GetFinalPathNameByHandle(handle, GetFinalPathNameByHandleFlags.FILE_NAME_NORMALIZED) .Should().Be(extendedPath); FileDesktopMethods.GetFinalPathNameByHandle(handle, GetFinalPathNameByHandleFlags.FILE_NAME_OPENED) .Should().Be(extendedPath); } using (var handle = FileMethods.CreateFile(symbolicLink.ToUpperInvariant(), DesiredAccess.FILE_GENERIC_READ, ShareMode.FILE_SHARE_READWRITE, CreationDisposition.OPEN_EXISTING, FileAttributes.NONE, FileFlags.FILE_FLAG_OPEN_REPARSE_POINT)) { handle.IsInvalid.Should().BeFalse(); FileDesktopMethods.GetFinalPathNameByHandle(handle, GetFinalPathNameByHandleFlags.FILE_NAME_NORMALIZED) .Should().Be(extendedLink); FileDesktopMethods.GetFinalPathNameByHandle(handle, GetFinalPathNameByHandleFlags.FILE_NAME_OPENED) .Should().Be(extendedLink); } } }