Example #1
0
        public void GetMultipleStreamInfoByHandle()
        {
            using (var temp = new TestFileCleaner())
            {
                string source = temp.GetTestPath();
                using (var file = FileMethods.CreateFile(source, DesiredAccess.GENERIC_READ, ShareMode.FILE_SHARE_READWRITE, CreationDisposition.CREATE_NEW))
                {
                    file.IsInvalid.Should().BeFalse();
                }

                string destination = temp.GetTestPath();
                FileMethods.CopyFile(source, destination);

                string alternateStream = destination + @":Foo:$DATA";
                FileMethods.CopyFile(source, alternateStream);

                using (var file = FileMethods.CreateFile(destination, DesiredAccess.GENERIC_READ | DesiredAccess.GENERIC_WRITE, ShareMode.FILE_SHARE_READWRITE, CreationDisposition.OPEN_EXISTING))
                {
                    var fileInfo = FileMethods.GetStreamInformationByHandle(file);
                    fileInfo.Should().BeEquivalentTo(new StreamInformation[]
                    {
                        new StreamInformation {
                            Name = @"::$DATA"
                        },
                        new StreamInformation {
                            Name = @":Foo:$DATA"
                        }
                    });
                }
            }
        }
        public void GetMultipleStreamInfoByHandle()
        {
            using (var temp = new TestFileCleaner())
            {
                string source = temp.GetTestPath();
                using (var file = FileMethods.CreateFile(source, CreationDisposition.CreateNew))
                {
                    file.IsInvalid.Should().BeFalse();
                }

                string destination = temp.GetTestPath();
                FileMethods.CopyFile(source, destination);

                string alternateStream = destination + @":Foo:$DATA";
                FileMethods.CopyFile(source, alternateStream);

                using (var file = FileMethods.CreateFile(destination, CreationDisposition.OpenExisting))
                {
                    var fileInfo = FileMethods.GetStreamInformation(file);
                    fileInfo.Should().BeEquivalentTo(new StreamInformation[]
                    {
                        new StreamInformation {
                            Name = @"::$DATA"
                        },
                        new StreamInformation {
                            Name = @":Foo:$DATA"
                        }
                    });
                }
            }
        }
Example #3
0
        /// <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);
                }
            }
        }
Example #4
0
        public void LoadAsBinaryFromLongPath()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string longPath = @"\\?\" + PathGenerator.CreatePathOfLength(cleaner.TempFolder, 500);
                FileHelper.CreateDirectoryRecursive(longPath);
                string longPathLibrary = Paths.Combine(longPath, "LoadAsBinaryFromLongPath.dll");
                FileMethods.CopyFile(GetNativeTestLibraryLocation(), longPathLibrary);

                using (var handle = ModuleDesktopMethods.LoadLibrary(longPathLibrary, LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH))
                {
                    handle.IsInvalid.Should().BeFalse();
                }
            }
        }
Example #5
0
        public void LoadFunctionFromLongPath()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string longPath = @"\\?\" + PathGenerator.CreatePathOfLength(cleaner.TempFolder, 500);
                FileHelper.CreateDirectoryRecursive(longPath);
                string longPathLibrary = Paths.Combine(longPath, "LoadFunctionFromLongPath.dll");
                FileMethods.CopyFile(GetNativeTestLibraryLocation(), longPathLibrary);

                using (var handle = ModuleDesktopMethods.LoadLibrary(longPathLibrary, 0))
                {
                    var doubler = ModuleDesktopMethods.GetFunctionDelegate <DoubleDelegate>(handle, "Double");
                    doubler(2).Should().Be(4);
                }
            }
        }
Example #6
0
        public void LoadStringFromLongPath()
        {
            using (var cleaner = new TestFileCleaner())
            {
                string longPath = @"\\?\" + PathGenerator.CreatePathOfLength(cleaner.TempFolder, 500);
                FileHelper.CreateDirectoryRecursive(longPath);
                string longPathLibrary = Paths.Combine(longPath, "LoadStringFromLongPath.dll");
                FileMethods.CopyFile(GetNativeTestLibraryLocation(), longPathLibrary);

                using (var handle = ModuleDesktopMethods.LoadLibrary(longPathLibrary,
                                                                     LoadLibraryFlags.LOAD_LIBRARY_AS_IMAGE_RESOURCE | LoadLibraryFlags.LOAD_LIBRARY_AS_DATAFILE))
                {
                    string resource = ResourceDesktopMethods.LoadString(handle, 101);
                    resource.Should().Be("Test");
                }
            }
        }
Example #7
0
        public void CopyFileBasic()
        {
            using (var temp = new TestFileCleaner())
            {
                string source = temp.GetTestPath();
                using (var file = FileMethods.CreateFile(source, DesiredAccess.GENERIC_READ, ShareMode.FILE_SHARE_READWRITE, CreationDisposition.CREATE_NEW))
                {
                    file.IsInvalid.Should().BeFalse();
                }

                string destination = temp.GetTestPath();
                FileMethods.CopyFile(source, destination);

                var info = FileMethods.GetFileAttributesEx(destination);
                info.Attributes.Should().NotHaveFlag(FileAttributes.FILE_ATTRIBUTE_DIRECTORY);
            }
        }
        public void CopyFileBasic()
        {
            using (var temp = new TestFileCleaner())
            {
                string source = temp.GetTestPath();
                using (var file = FileMethods.CreateFile(source, CreationDisposition.CreateNew, DesiredAccess.GenericRead))
                {
                    file.IsInvalid.Should().BeFalse();
                }

                string destination = temp.GetTestPath();
                FileMethods.CopyFile(source, destination);

                var info = FileMethods.GetFileAttributesEx(destination);
                info.dwFileAttributes.Should().NotHaveFlag(FileAttributes.Directory);
            }
        }