public void CreateSymbolicLinkToFile() { using (var cleaner = new TestFileCleaner()) { string filePath = cleaner.CreateTestFile("CreateSymbolicLinkToFile"); string symbolicLink = cleaner.GetTestPath(); Action action = () => FileMethods.CreateSymbolicLink(symbolicLink, filePath); if (CanCreateSymbolicLinks()) { action(); var attributes = FileMethods.GetFileAttributes(symbolicLink); attributes.Should().HaveFlag(FileAttributes.ReparsePoint); using (var handle = FileMethods.CreateFile(symbolicLink, CreationDisposition.OpenExisting, DesiredAccess.ReadExtendedAttributes, ShareModes.All, fileFlags: FileFlags.OpenReparsePoint)) { handle.IsInvalid.Should().BeFalse(); var(printName, substituteName, tag) = DeviceMethods.GetReparsePointNames(handle); tag.Should().Be(ReparseTag.SymbolicLink); printName.Should().Be(filePath); substituteName.Should().Be(@"\??\" + filePath); } } else { // Can't create links unless you have admin rights SE_CREATE_SYMBOLIC_LINK_NAME SeCreateSymbolicLinkPrivilege action.ShouldThrow <System.IO.IOException>().And.HResult.Should().Be((int)ErrorMacros.HRESULT_FROM_WIN32(WindowsError.ERROR_PRIVILEGE_NOT_HELD)); } } }
public void GetFileAttributesLongPath() { string longPath = @"\\?\" + PathGenerator.CreatePathOfLength(@"C:\", 300); Action action = () => FileMethods.GetFileAttributes(longPath); action.ShouldThrow <System.IO.DirectoryNotFoundException>(); }
public void CreateSymbolicLinkToLongPathFile() { using (var cleaner = new TestFileCleaner()) { string longPath = @"\\?\" + PathGenerator.CreatePathOfLength(cleaner.TempFolder, 500); FileHelper.CreateDirectoryRecursive(longPath); string filePath = cleaner.CreateTestFile("CreateSymbolicLinkToLongPathFile", longPath); string symbolicLink = cleaner.GetTestPath(); Action action = () => FileMethods.CreateSymbolicLink(symbolicLink, filePath); if (CanCreateSymbolicLinks()) { action(); var attributes = FileMethods.GetFileAttributes(symbolicLink); attributes.Should().HaveFlag(FileAttributes.ReparsePoint); } else { action.ShouldThrow <System.IO.IOException>().And.HResult.Should().Be((int)ErrorMacros.HRESULT_FROM_WIN32(WindowsError.ERROR_PRIVILEGE_NOT_HELD)); } } }