コード例 #1
0
        private void ProcessFile(bool isCopyAction, string sourceFilePath, string targetFilePath,
                                 DirectoryOperationsShared.ExistsOption existsOption, bool replaceExistingFile)
        {
            var destinationFileExist = System.IO.File.Exists(targetFilePath);

            if (destinationFileExist &&
                ((existsOption == DirectoryOperationsShared.ExistsOption.MergeDirectory && !replaceExistingFile) ||
                 (existsOption == DirectoryOperationsShared.ExistsOption.DoNothing)))
            {
                Log(string.Format("File {0} exists. Skip.", targetFilePath));
                return;
            }

            if (destinationFileExist &&
                ((existsOption == DirectoryOperationsShared.ExistsOption.MergeDirectory && replaceExistingFile) ||
                 (existsOption == DirectoryOperationsShared.ExistsOption.OverwriteDirectory)))
            {
                Log("Delete file " + targetFilePath);
                System.IO.File.Delete(targetFilePath);
            }

            if (isCopyAction)
            {
                Log(string.Format("Copy {0} to {1}", sourceFilePath, targetFilePath));
                System.IO.File.Copy(sourceFilePath, targetFilePath);
            }
            else
            {
                Log(string.Format("Move {0} to {1}", sourceFilePath, targetFilePath));
                System.IO.File.Move(sourceFilePath, targetFilePath);
            }
        }
コード例 #2
0
        public void CopyOrMove(DirectoryOperationsShared.ActionType action,
                               string sourceDirectoryPath,
                               string targetDirectoryPath,
                               DirectoryOperationsShared.ExistsOption existsOption,
                               bool replaceExistingFile)
        {
            if (string.IsNullOrEmpty(sourceDirectoryPath))
            {
                throw new Exception("Source directory path cannot be null or empty.");
            }

            if (string.IsNullOrEmpty(targetDirectoryPath))
            {
                throw new Exception("Target directory path cannot be null or empty.");
            }

            if (!Directory.Exists(sourceDirectoryPath))
            {
                throw new DirectoryNotFoundException(string.Format("Source directory [{0}] does not exist.", sourceDirectoryPath));
            }

            sourceDirectoryPath = sourceDirectoryPath.TrimEnd('\\');
            targetDirectoryPath = targetDirectoryPath.TrimEnd('\\');

            PerformAction((action == DirectoryOperationsShared.ActionType.Copy), sourceDirectoryPath, targetDirectoryPath, existsOption, replaceExistingFile);
        }
コード例 #3
0
        private void PerformAction(bool isCopyAction, string sourceDirectoryPath, string targetDirectoryPath,
                                   DirectoryOperationsShared.ExistsOption existsOption, bool replaceExistingFile)
        {
            if (IsValidDoNothingOption(isCopyAction, sourceDirectoryPath, targetDirectoryPath, existsOption))
            {
                return;
            }

            ProcessDirectory(isCopyAction, sourceDirectoryPath, targetDirectoryPath, existsOption, replaceExistingFile, isCopyAction);
        }
コード例 #4
0
        private void ProcessFiles(bool isCopyAction, string sourceDirectoryPath, string targetDirectoryPath,
                                  DirectoryOperationsShared.ExistsOption existsOption, bool replaceExistingFile)
        {
            foreach (string nextFilePath in Directory.EnumerateFiles(sourceDirectoryPath, "*", SearchOption.TopDirectoryOnly))
            {
                var fileName        = Path.GetFileName(nextFilePath);
                var destinationPath = Path.Combine(targetDirectoryPath, fileName);

                ProcessFile(isCopyAction, nextFilePath, destinationPath, existsOption, replaceExistingFile);
            }
        }
コード例 #5
0
        private void PerformAction(bool isCopyAction, string sourceDirectoryPath, string targetDirectoryPath,
                                   DirectoryOperationsShared.ExistsOption existsOption, bool replaceExistingFile)
        {
            string leastFolderName        = Path.GetFileNameWithoutExtension(sourceDirectoryPath);
            string targetSubDirectoryPath = Path.Combine(targetDirectoryPath, leastFolderName);

            if (Directory.Exists(targetSubDirectoryPath) && existsOption == DirectoryOperationsShared.ExistsOption.DoNothing)
            {
                Log(string.Format("Directory {0} exists. Do nothing.", targetSubDirectoryPath));
                return;
            }

            ProcessDirectory(isCopyAction, sourceDirectoryPath, targetDirectoryPath, existsOption, replaceExistingFile);
        }
コード例 #6
0
        private void ProcessDirectory(bool isCopyAction, string sourceDirectoryPath, string targetDirectoryPath,
                                      DirectoryOperationsShared.ExistsOption existsOption, bool replaceExistingFile)
        {
            var leastFolderName            = Path.GetFileNameWithoutExtension(sourceDirectoryPath);
            var currentTargetDirectoryPath = Path.Combine(targetDirectoryPath, leastFolderName);

            Log("Create directory " + currentTargetDirectoryPath);
            Directory.CreateDirectory(currentTargetDirectoryPath);

            foreach (string nextSourceDirectory in Directory.EnumerateDirectories(sourceDirectoryPath))
            {
                ProcessDirectory(isCopyAction, nextSourceDirectory, currentTargetDirectoryPath, existsOption, replaceExistingFile);
            }

            ProcessFiles(isCopyAction, sourceDirectoryPath, currentTargetDirectoryPath, existsOption, replaceExistingFile);

            if (!Directory.GetFileSystemEntries(sourceDirectoryPath, "*", SearchOption.AllDirectories).Any() && !isCopyAction)
            {
                Log("Delete directory " + sourceDirectoryPath);
                Directory.Delete(sourceDirectoryPath);
            }
        }
コード例 #7
0
        private bool IsValidDoNothingOption(bool isCopyAction, string sourceDirectoryPath, string targetDirectoryPath, DirectoryOperationsShared.ExistsOption existsOption)
        {
            string leastFolderName        = Path.GetFileNameWithoutExtension(sourceDirectoryPath);
            string targetSubDirectoryPath = (isCopyAction) ? Path.Combine(targetDirectoryPath, leastFolderName) : targetDirectoryPath;

            if (Directory.Exists(targetSubDirectoryPath) && existsOption == DirectoryOperationsShared.ExistsOption.DoNothing)
            {
                Log(string.Format("Directory {0} exists. Do nothing.", targetSubDirectoryPath));
                return(true);
            }

            return(false);
        }