Exemplo n.º 1
0
        private void File_SetAttributes(bool isNetwork)
        {
            using (var tempRoot = new TemporaryDirectory(isNetwork))
            {
                var folder = tempRoot.CreateRecursiveTree(5);

                Console.WriteLine("Input Directory Path: [{0}]", folder);


                foreach (var fullPath in System.IO.Directory.EnumerateFileSystemEntries(folder.FullName, "*", System.IO.SearchOption.AllDirectories))
                {
                    System.IO.File.SetAttributes(fullPath, System.IO.FileAttributes.Normal);

                    const System.IO.FileAttributes attributes = System.IO.FileAttributes.Hidden | System.IO.FileAttributes.Archive | System.IO.FileAttributes.System | System.IO.FileAttributes.ReadOnly;


                    Alphaleonis.Win32.Filesystem.File.SetAttributes(fullPath, attributes);


                    var sysIO = System.IO.File.GetAttributes(fullPath);

                    // Remove Directory attribute for comparing.
                    var noDir = sysIO;
                    noDir &= ~System.IO.FileAttributes.Directory;


                    Assert.AreEqual(attributes, noDir);

                    Assert.AreEqual(sysIO, Alphaleonis.Win32.Filesystem.File.GetAttributes(fullPath));
                }
            }

            Console.WriteLine();
        }
        private void AlphaFS_Directory_Copy_ExistingDirectory_WithProgress(bool isNetwork)
        {
            using (var tempRoot = new TemporaryDirectory(isNetwork))
            {
                var folderSrc = tempRoot.CreateRecursiveTree(5);
                var folderDst = tempRoot.RandomDirectoryFullPath;

                Console.WriteLine("Src Directory Path: [{0}]", folderSrc.FullName);
                Console.WriteLine("Dst Directory Path: [{0}]", folderDst);


                var dirEnumOptions = Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.FilesAndFolders | Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.Recursive;

                var props = Alphaleonis.Win32.Filesystem.Directory.GetProperties(folderSrc.FullName, dirEnumOptions);

                var sourceTotal      = props["Total"];
                var sourceTotalFiles = props["File"];
                var sourceTotalSize  = props["Size"];

                Console.WriteLine("\n\tTotal size: [{0}] - Total Folders: [{1}] - Files: [{2}]", Alphaleonis.Utils.UnitSizeToText(sourceTotalSize), sourceTotal - sourceTotalFiles, sourceTotalFiles);


                // The copy progress handler.
                var callback = new Alphaleonis.Win32.Filesystem.CopyMoveProgressRoutine(FolderCopyProgressHandler);

                Console.WriteLine();


                var copyResult = Alphaleonis.Win32.Filesystem.Directory.Copy(folderSrc.FullName, folderDst, Alphaleonis.Win32.Filesystem.CopyOptions.FailIfExists, callback, null);

                UnitTestConstants.Dump(copyResult, -18);


                props = Alphaleonis.Win32.Filesystem.Directory.GetProperties(folderDst, dirEnumOptions);
                Assert.AreEqual(sourceTotal, props["Total"], "The number of total file system objects do not match.");
                Assert.AreEqual(sourceTotalFiles, props["File"], "The number of total files do not match.");
                Assert.AreEqual(sourceTotalSize, props["Size"], "The total file size does not match.");
                Assert.AreNotEqual(null, copyResult);


                // Test against copyResult results.

                Assert.AreEqual(sourceTotal, copyResult.TotalFolders + copyResult.TotalFiles, "The number of total file system objects do not match.");
                Assert.AreEqual(sourceTotalFiles, copyResult.TotalFiles, "The number of total files do not match.");
                Assert.AreEqual(sourceTotalSize, copyResult.TotalBytes, "The total file size does not match.");
                Assert.IsTrue(copyResult.IsCopy);
                Assert.IsFalse(copyResult.IsMove);
                Assert.IsTrue(copyResult.IsDirectory);
                Assert.IsFalse(copyResult.IsFile);

                Assert.IsTrue(System.IO.Directory.Exists(folderSrc.FullName), "The original directory does not exist, but is expected to.");
            }


            Console.WriteLine();
        }
        private void AlphaFS_Directory_Move_ToDifferentVolume_EmulateMoveUsingCopyDelete(bool isNetwork)
        {
            // Do not pass isNetwork, as to always use UnitTestConstants.TempPath

            using (var tempRoot = new TemporaryDirectory())
            {
                var folderSrc = isNetwork ? tempRoot.CreateRecursiveTree(5, Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempRoot.RandomDirectoryFullPath)) : tempRoot.CreateRecursiveTree(5);

                var folderDst = !isNetwork ? new System.IO.DirectoryInfo(Alphaleonis.Win32.Filesystem.Path.LocalToUnc(tempRoot.RandomDirectoryFullPath)).FullName : tempRoot.RandomDirectoryFullPath;


                Console.WriteLine("Src Directory Path: [{0}]", folderSrc.FullName);
                Console.WriteLine("Dst Directory Path: [{0}]", folderDst);


                var dirEnumOptions = Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.FilesAndFolders | Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.Recursive;

                var props            = Alphaleonis.Win32.Filesystem.Directory.GetProperties(folderSrc.FullName, dirEnumOptions);
                var sourceTotal      = props["Total"];
                var sourceTotalFiles = props["File"];
                var sourceTotalSize  = props["Size"];

                Console.WriteLine("\n\tTotal size: [{0}] - Total Folders: [{1}] - Files: [{2}]", Alphaleonis.Utils.UnitSizeToText(sourceTotalSize), sourceTotal - sourceTotalFiles, sourceTotalFiles);



                var moveResult = Alphaleonis.Win32.Filesystem.Directory.Move(folderSrc.FullName, folderDst, Alphaleonis.Win32.Filesystem.MoveOptions.CopyAllowed);

                UnitTestConstants.Dump(moveResult);

                props = Alphaleonis.Win32.Filesystem.Directory.GetProperties(folderDst, dirEnumOptions);
                Assert.AreEqual(sourceTotal, props["Total"], "The number of total file system objects do not match.");
                Assert.AreEqual(sourceTotalFiles, props["File"], "The number of total files do not match.");
                Assert.AreEqual(sourceTotalSize, props["Size"], "The total file size does not match.");


                // Test against moveResult results.

                var isMove = moveResult.IsMove;

                Assert.AreEqual(sourceTotal, moveResult.TotalFolders + moveResult.TotalFiles, "The number of total file system objects do not match.");
                Assert.AreEqual(sourceTotalFiles, moveResult.TotalFiles, "The number of total files do not match.");
                Assert.AreEqual(sourceTotalSize, moveResult.TotalBytes, "The total file size does not match.");
                Assert.IsFalse(isMove, "The action was expected to be a Copy, not a Move.");
                Assert.IsTrue(moveResult.IsEmulatedMove, "The action was expected to be emulated (Copy + Delete).");

                Assert.IsFalse(System.IO.Directory.Exists(folderSrc.FullName), "The original folder exists, but is expected not to.");
            }

            Console.WriteLine();
        }
Exemplo n.º 4
0
        private void Directory_Move_SameVolume(bool isNetwork)
        {
            using (var tempRoot = new TemporaryDirectory(isNetwork))
            {
                var srcFolder = tempRoot.CreateRecursiveTree(5);
                var dstFolder = tempRoot.RandomDirectoryFullPath;

                Console.WriteLine("Src Directory Path: [{0}]", srcFolder.FullName);
                Console.WriteLine("Dst Directory Path: [{0}]", dstFolder);


                var dirEnumOptions = Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.FilesAndFolders | Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.Recursive;

                var props            = Alphaleonis.Win32.Filesystem.Directory.GetProperties(srcFolder.FullName, dirEnumOptions);
                var sourceTotal      = props["Total"];
                var sourceTotalFiles = props["File"];
                var sourceTotalSize  = props["Size"];

                Console.WriteLine("\n\tTotal size: [{0}] - Total Folders: [{1}] - Files: [{2}]", Alphaleonis.Utils.UnitSizeToText(sourceTotalSize), sourceTotal - sourceTotalFiles, sourceTotalFiles);


                var moveResult = Alphaleonis.Win32.Filesystem.Directory.Move(srcFolder.FullName, dstFolder, Alphaleonis.Win32.Filesystem.PathFormat.FullPath);

                UnitTestConstants.Dump(moveResult);

                props = Alphaleonis.Win32.Filesystem.Directory.GetProperties(dstFolder, dirEnumOptions);
                Assert.AreEqual(sourceTotal, props["Total"], "The number of total file system objects do not match.");
                Assert.AreEqual(sourceTotalFiles, props["File"], "The number of total files do not match.");
                Assert.AreEqual(sourceTotalSize, props["Size"], "The total file size does not match.");


                // Test against moveResult results.

                var isMove = moveResult.IsMove;

                Assert.AreEqual(isMove ? 1 : sourceTotal, moveResult.TotalFolders + moveResult.TotalFiles, "The number of total file system objects do not match.");
                Assert.AreEqual(isMove ? 0 : sourceTotalFiles, moveResult.TotalFiles, "The number of total files do not match.");
                Assert.AreEqual(isMove ? 0 : sourceTotalSize, moveResult.TotalBytes, "The total file size does not match.");
                Assert.IsTrue(isMove, "The action was expected to be a Move, not a Copy.");
                Assert.IsFalse(moveResult.IsCopy);
                Assert.IsTrue(moveResult.IsMove);
                Assert.IsTrue(moveResult.IsDirectory);
                Assert.IsFalse(moveResult.IsFile);

                Assert.IsFalse(System.IO.Directory.Exists(srcFolder.FullName), "The original folder exists, but is expected not to.");
            }


            Console.WriteLine();
        }
        private void AlphaFS_Directory_DeleteEmptySubdirectories(bool isNetwork)
        {
            using (var tempRoot = new TemporaryDirectory(isNetwork))
            {
                const int maxDepth             = 10;
                const int totalDirectories     = maxDepth * maxDepth + maxDepth;      // maxDepth = 10: 110 directories and 110 files.
                const int emptyDirectories     = maxDepth * maxDepth / 2;             // 50 empty directories.
                const int remainingDirectories = totalDirectories - emptyDirectories; // 60 remaining directories.

                var folder = tempRoot.CreateRecursiveTree(maxDepth);

                Console.WriteLine("Input Directory Path: [{0}]", folder.FullName);


                var searchPattern = Alphaleonis.Win32.Filesystem.Path.WildcardStarMatchAll;
                const Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions enumOptionsFolder = Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.Folders | Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.Recursive | Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.ContinueOnException;
                const Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions enumOptionsFile   = Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.Files | Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.Recursive | Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.ContinueOnException;


                var dirs0  = Alphaleonis.Win32.Filesystem.Directory.CountFileSystemObjects(folder.FullName, searchPattern, enumOptionsFolder);
                var files0 = Alphaleonis.Win32.Filesystem.Directory.CountFileSystemObjects(folder.FullName, searchPattern, enumOptionsFile);
                Console.WriteLine("\n\tCounted Directories: [{0}]  Empty Directories: [{1}]", dirs0, emptyDirectories);
                Console.WriteLine("\tCounted Files      : [{0}]", files0);


                Alphaleonis.Win32.Filesystem.Directory.DeleteEmptySubdirectories(folder.FullName, true);


                Assert.IsTrue(System.IO.Directory.Exists(folder.FullName), "The root directory does not exist, but is expected to.");

                var dirs1  = Alphaleonis.Win32.Filesystem.Directory.CountFileSystemObjects(folder.FullName, searchPattern, enumOptionsFolder);
                var files1 = Alphaleonis.Win32.Filesystem.Directory.CountFileSystemObjects(folder.FullName, searchPattern, enumOptionsFile);
                Console.WriteLine("\n\tCounted Directories: [{0}]", dirs1);
                Console.WriteLine("\tCounted Files      : [{0}]", files1);

                Assert.AreNotEqual(dirs0, dirs1, "The number of directories are equal, but are expected not to.");
                Assert.AreEqual(remainingDirectories, dirs1, "The number of directories are not equal, but are expected to be.");
                Assert.AreEqual(files0, files1, "The number of files are not equal, but are expected to be.");
                Assert.AreEqual(totalDirectories, emptyDirectories + remainingDirectories, "The number of directories are not equal, but are expected to be.");
            }

            Console.WriteLine();
        }
        private void AlphaFS_Directory_CountFileSystemObjects_FoldersOnly_Recursive(bool isNetwork)
        {
            using (var tempRoot = new TemporaryDirectory(isNetwork))
            {
                const int expectedFso        = 10;
                const int maxFso             = 10;
                const int expectedSubfolders = expectedFso * expectedFso + expectedFso;

                var folder = tempRoot.CreateRecursiveTree(maxFso);

                Console.WriteLine("Input Directory Path: [{0}]", folder.FullName);


                var fsoCount = Alphaleonis.Win32.Filesystem.Directory.CountFileSystemObjects(folder.FullName, "*", Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.Folders | Alphaleonis.Win32.Filesystem.DirectoryEnumerationOptions.Recursive);


                Console.WriteLine("\n\tTotal file system objects: [{0}]", fsoCount);

                Assert.AreEqual(expectedSubfolders, fsoCount, string.Format(CultureInfo.InvariantCulture, "The number of file system objects: {0} is not equal than expected: {1}", expectedSubfolders, fsoCount));
            }

            Console.WriteLine();
        }