Exemplo n.º 1
0
        public void CreateSymbolicLinkToLongPathFile()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string filePath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName());
                IExtendedFileService fileService = new FileService();
                fileService.WriteAllText(filePath, "CreateSymbolicLinkToFile");

                string symbolicLink = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName());
                Action action = () => NativeMethods.FileManagement.CreateSymbolicLink(symbolicLink, filePath);

                if (fileService.CanCreateSymbolicLinks())
                {
                    action();
                    var attributes = NativeMethods.FileManagement.GetFileAttributes(symbolicLink);
                    attributes.Should().HaveFlag(FileAttributes.ReparsePoint);
                }
                else
                {
                    action.ShouldThrow<IOException>().And.HResult.Should().Be(NativeErrorHelper.GetHResultForWindowsError(NativeMethods.WinError.ERROR_PRIVILEGE_NOT_HELD));
                }
            }
        }
Exemplo n.º 2
0
        public void FinalPathNameLinkBehavior()
        {
            IExtendedFileService fileService = new FileService();
            if (!fileService.CanCreateSymbolicLinks()) return;

            // GetFinalPathName always points to the linked file
            using (var cleaner = new TestFileCleaner())
            {
                string filePath = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName());
                fileService.WriteAllText(filePath, "CreateSymbolicLinkToFile");

                string symbolicLink = Paths.Combine(cleaner.TempFolder, Path.GetRandomFileName());
                NativeMethods.FileManagement.CreateSymbolicLink(symbolicLink, filePath);
                NativeMethods.FileManagement.FileExists(symbolicLink).Should().BeTrue("symbolic link should exist");

                using (var handle = NativeMethods.FileManagement.CreateFile(symbolicLink, FileAccess.Read, FileShare.ReadWrite, FileMode.Open, 0))
                {
                    handle.IsInvalid.Should().BeFalse();
                    NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.FILE_NAME_NORMALIZED)
                        .Should().Be(filePath);
                    NativeMethods.FileManagement.GetFinalPathName(handle, NativeMethods.FileManagement.FinalPathFlags.FILE_NAME_OPENED)
                        .Should().Be(filePath);
                }
            }
        }