Ejemplo n.º 1
0
        public static void MoveDirectory(
            string source,
            string target,
            DirectoryExistsPolicy directoryPolicy = DirectoryExistsPolicy.Fail,
            FileExistsPolicy filePolicy           = FileExistsPolicy.Fail,
            bool deleteRemainingFiles             = false)
        {
            ControlFlow.Assert(!Directory.Exists(target) || directoryPolicy != DirectoryExistsPolicy.Fail,
                               $"!Directory.Exists({target}) || policy != DirectoryExistsPolicy.Fail");

            Logger.Info($"Moving directory from '{source}' to '{target}'...");
            if (!Directory.Exists(target))
            {
                Directory.Move(source, target);
            }
            else
            {
                Directory.GetDirectories(source).ForEach(x => MoveDirectoryToDirectory(x, target, directoryPolicy, filePolicy));
                Directory.GetFiles(source).ForEach(x => MoveFileToDirectory(x, target, filePolicy));

                if (!new DirectoryInfo(source).EnumerateFileSystemInfos().Any() || deleteRemainingFiles)
                {
                    DeleteDirectoryInternal(source);
                }
            }
        }
Ejemplo n.º 2
0
 public static void RenameDirectory(
     string directory,
     string newName,
     DirectoryExistsPolicy directoryPolicy = DirectoryExistsPolicy.Fail,
     FileExistsPolicy filePolicy           = FileExistsPolicy.Fail)
 {
     MoveDirectory(directory, Path.Combine(Path.GetDirectoryName(directory).NotNull(), newName), directoryPolicy, filePolicy);
 }
Ejemplo n.º 3
0
 public static void MoveDirectoryToDirectory(
     string source,
     string targetDirectory,
     DirectoryExistsPolicy directoryPolicy = DirectoryExistsPolicy.Fail,
     FileExistsPolicy filePolicy           = FileExistsPolicy.Fail)
 {
     MoveDirectory(source, Path.Combine(targetDirectory, new DirectoryInfo(source).Name), directoryPolicy, filePolicy);
 }
Ejemplo n.º 4
0
        public static void CopyDirectoryRecursively(
            string source,
            string target,
            DirectoryExistsPolicy directoryPolicy       = DirectoryExistsPolicy.Fail,
            FileExistsPolicy filePolicy                 = FileExistsPolicy.Fail,
            Func <DirectoryInfo, bool> excludeDirectory = null,
            Func <FileInfo, bool> excludeFile           = null)
        {
            ControlFlow.Assert(Directory.Exists(source), $"Directory.Exists({source})");
            ControlFlow.Assert(!PathConstruction.IsDescendantPath(source, target),
                               $"Target directory '{target}' must not be in source directory '{source}'.");
            //ControlFlow.Assert(!Contains(source, target), $"Target '{target}' is not contained in source '{source}'.");

            Logger.Info($"Recursively copying from '{source}' to '{target}'...");
            CopyRecursivelyInternal(source, target, directoryPolicy, filePolicy, excludeDirectory, excludeFile);
        }
Ejemplo n.º 5
0
        private static void CopyRecursivelyInternal(
            string source,
            string target,
            DirectoryExistsPolicy directoryPolicy,
            FileExistsPolicy filePolicy,
            [CanBeNull] Func <DirectoryInfo, bool> excludeDirectory,
            [CanBeNull] Func <FileInfo, bool> excludeFile)
        {
            if (excludeDirectory != null && excludeDirectory(new DirectoryInfo(source)))
            {
                return;
            }

            ControlFlow.Assert(!Directory.Exists(target) || directoryPolicy != DirectoryExistsPolicy.Fail,
                               $"!Directory.Exists({target}) || directoryPolicy != DirectoryExistsPolicy.Fail");

            string GetDestinationPath(string path)
            => Path.Combine(target, PathConstruction.GetRelativePath(source, path));

            Directory.CreateDirectory(target);
            foreach (var sourceFile in Directory.GetFiles(source))
            {
                if (excludeFile != null && excludeFile(new FileInfo(sourceFile)))
                {
                    continue;
                }

                var targetFile = GetDestinationPath(sourceFile);
                if (!ShouldCopyFile(sourceFile, targetFile, filePolicy))
                {
                    continue;
                }

                //EnsureFileAttributes(sourceFile);
                File.Copy(sourceFile, targetFile, overwrite: true);
            }

            Directory.GetDirectories(source)
            .ForEach(x => CopyRecursivelyInternal(
                         x,
                         GetDestinationPath(x),
                         directoryPolicy,
                         filePolicy,
                         excludeDirectory,
                         excludeFile));
        }
Ejemplo n.º 6
0
        public static void MoveDirectory(
            string source,
            string target,
            DirectoryExistsPolicy directoryPolicy = DirectoryExistsPolicy.Fail,
            FileExistsPolicy filePolicy           = FileExistsPolicy.Fail)
        {
            ControlFlow.Assert(!Directory.Exists(target) || directoryPolicy != DirectoryExistsPolicy.Fail,
                               $"!Directory.Exists({target}) || policy != DirectoryExistsPolicy.Fail");

            Logger.Info($"Moving directory from '{source}' to '{target}'...");
            if (!Directory.Exists(target))
            {
                Directory.Move(source, target);
            }
            else
            {
                Directory.GetDirectories(source).ForEach(x => MoveDirectoryToDirectory(x, target, directoryPolicy, filePolicy));
                Directory.GetFiles(source).ForEach(x => MoveFileToDirectory(x, target, filePolicy));
                Directory.Delete(source);
            }
        }
Ejemplo n.º 7
0
        public static void MoveDirectory(
            string source,
            string target,
            DirectoryExistsPolicy directoryPolicy = DirectoryExistsPolicy.Fail,
            FileExistsPolicy filePolicy = FileExistsPolicy.Fail,
            bool deleteRemainingFiles = false)
        {
            Assert.True(!Directory.Exists(target) || directoryPolicy != DirectoryExistsPolicy.Fail);
            Log.Information("Moving directory from {Source} to {Target}...", source, target);
            if (!Directory.Exists(target))
            {
                Directory.Move(source, target);
            }
            else
            {
                Directory.GetDirectories(source).ForEach(x => MoveDirectoryToDirectory(x, target, directoryPolicy, filePolicy));
                Directory.GetFiles(source).ForEach(x => MoveFileToDirectory(x, target, filePolicy));

                if (!new DirectoryInfo(source).EnumerateFileSystemInfos().Any() || deleteRemainingFiles)
                    DeleteDirectoryInternal(source);
            }
        }