Exemple #1
0
        /// <summary>
        /// Creates a new unique filename based off of the given file name.
        /// </summary>
        /// <param name="aPath">The path to the file.</param>
        /// <returns>New unique file name.</returns>
        public static String MakeUnique(String aPath)
        {
            if (aPath.isEmpty())
            {
                return(String.Empty);
            }
            String directory = Path.GetDirectoryName(aPath);
            String filename  = Path.GetFileNameWithoutExtension(aPath) + "_1";
            String ext       = Path.GetExtension(aPath);

            while (File.Exists(PathExt.Combine(directory, filename + ext)))
            {
                filename += "_1";
            }
            return(PathExt.Combine(directory, filename + ext));
        }
Exemple #2
0
 /// <summary>
 /// Copy a file from on
 /// </summary>
 /// <param name="aSourceDir">The source directory.</param>
 /// <param name="aDestinationDir">The destination directory</param>
 /// <param name="aFilename">The name of the file</param>
 /// <param name="aNameConflictOption">How to handle conflicts.</param>
 /// <param name="aHandler">Callback function to handle progress notifications</param>
 /// <param name="aCancel"></param>
 public static void Copy(String aSourceDirectory, String aDestinationDirectory, String aFilename, NameConflictOption aNameConflictOption, Action <ProcessProgress> aHandler, ref int aCancel)
 {
     Copy(PathExt.Combine(aSourceDirectory, aFilename), PathExt.Combine(aDestinationDirectory, aFilename), aNameConflictOption, aHandler, ref aCancel);
 }
Exemple #3
0
        /// <summary>
        /// Compare one source directory with multiple destination directories
        /// </summary>
        /// <param name="sourceDir">The source directory to compare</param>
        /// <param name="compareDir">The destination directories to compare</param>
        /// <param name="searchOption">If subdirectories should also be compared</param>
        /// <returns></returns>
        public List <ComparisonResult> CompareDirs(String sourceDir, IEnumerable <String> compareDirs, DirectorySearchOption searchOption)
        {
            List <DestinationInfo> directories         = new List <DestinationInfo>();
            IEnumerator <String>   comparisonDirectory = compareDirs.GetEnumerator();

            while (comparisonDirectory.MoveNext())
            {
                directories.Add(new DestinationInfo(comparisonDirectory.Current));
            }

            bool bailOut = false;
            List <ComparisonResult> results = new List <ComparisonResult>();

            if (!Directory.Exists(sourceDir))
            {
                ComparisonResult result = new ComparisonResult(sourceDir, sourceDir, ComparisonResult.DifferenceType.SourceMissing, false);
                results.Add(result);
                sendEvent(result);
                bailOut = true;
            }
            List <ComparisonResult.Destination> missingDestinationDirectoires = new List <ComparisonResult.Destination>();

            foreach (DestinationInfo dir in directories.Where(d => !d.Done))
            {
                if (!Directory.Exists(dir.Path))
                {
                    missingDestinationDirectoires.Add(new ComparisonResult.Destination(dir.Path, ComparisonResult.DifferenceType.DestMissing));
                    dir.Done = true;
                }
            }
            if (missingDestinationDirectoires.Count > 0)
            {
                ComparisonResult compareResult = new ComparisonResult(sourceDir, missingDestinationDirectoires, false);
                sendEvent(compareResult);
                results.Add(compareResult);
            }

            if (bailOut || directories.All(d => d.Done))
            {
                return(results);
            }

            // First compare all the files
            List <String>   sourceFiles = DirectoryExt.GetFiles(sourceDir, searchOption);
            List <String>   sourceDirs  = DirectoryExt.GetDirectories(sourceDir, searchOption).ToList();
            ProcessProgress progress    = null;

            if (!HandleDirectoryCompareProgress.isNull())
            {
                progress = new ProcessProgress(sourceFiles.Count + sourceDirs.Count, (prog) => HandleDirectoryCompareProgress(prog));
            }
            else
            {
                progress = new ProcessProgress(sourceFiles.Count + sourceDirs.Count);
            }

            foreach (DestinationInfo dir in directories.Where(d => !d.Done))
            {
                dir.FileList = DirectoryExt.GetFiles(dir.Path, searchOption).ToList();
            }


            for (int i = 0; i < sourceFiles.Count; ++i)
            {
                if (mCancelRequested)
                {
                    break;
                }
                List <ComparisonResult.Destination> destinationResults = new List <ComparisonResult.Destination>();
                String        sourceFileName = sourceFiles[i];
                List <String> filePaths      = new List <string>();
                foreach (DestinationInfo dir in directories.Where(d => !d.Done))
                {
                    String destFileName = PathExt.Combine(dir.Path, PathExt.GetRelativePath(sourceDir, sourceFileName));
                    if (dir.FileList.Contains(destFileName))
                    {
                        filePaths.Add(destFileName);
                        dir.FileList.Remove(destFileName);
                    }
                    else
                    {
                        destinationResults.Add(new ComparisonResult.Destination(destFileName, ComparisonResult.DifferenceType.DestMissing));
                    }
                }
                progress.Processed++;
                ComparisonResult compareResult = CompareFiles(sourceFileName, filePaths);
                compareResult.Destinations.AddRange(destinationResults);
                if (compareResult.Destinations.Count > 0)
                {
                    sendEvent(compareResult);
                    results.Add(compareResult);
                }
            }

            foreach (DestinationInfo dir in directories.Where(d => !d.Done))
            {
                foreach (String file in dir.FileList)
                {
                    if (mCancelRequested)
                    {
                        break;
                    }
                    ComparisonResult compareResult = new ComparisonResult(PathExt.Combine(sourceDir, PathExt.GetRelativePath(dir.Path, file)), file, ComparisonResult.DifferenceType.SourceMissing, true);
                    sendEvent(compareResult);
                    results.Add(compareResult);
                }
            }

            //Next compare the directories
            foreach (DestinationInfo dir in directories.Where(d => !d.Done))
            {
                if (mCancelRequested)
                {
                    break;
                }
                dir.DirList = DirectoryExt.GetDirectories(dir.Path, searchOption).ToList();
            }

            for (int i = 0; i < sourceDirs.Count; ++i)
            {
                foreach (DestinationInfo dir in directories.Where(d => !d.Done))
                {
                    if (mCancelRequested)
                    {
                        break;
                    }
                    String sourceDirName = sourceDirs[i];
                    String destDirName   = PathExt.Combine(dir.Path, PathExt.GetRelativePath(sourceDir, sourceDirName));
                    if (dir.DirList.Contains(destDirName))
                    {
                        dir.DirList.Remove(destDirName);
                    }
                    else
                    {
                        // Only send a difference if the directory is empty, otherwise the file comparison will cover this case.
                        if (DirectoryExt.Empty(sourceDirName))
                        {
                            ComparisonResult compareResult = new ComparisonResult(sourceDirName, destDirName, ComparisonResult.DifferenceType.DestMissing, false);
                            sendEvent(compareResult);
                            results.Add(compareResult);
                        }
                    }
                }
                progress.Processed++;
            }

            foreach (DestinationInfo directory in directories.Where(d => !d.Done))
            {
                foreach (String dir in directory.DirList)
                {
                    if (mCancelRequested)
                    {
                        break;
                    }
                    // Only send a difference if the directory is empty, otherwise the file comparison will cover this case.
                    if (DirectoryExt.Empty(dir))
                    {
                        ComparisonResult compareResult = new ComparisonResult(PathExt.Combine(sourceDir, PathExt.GetRelativePath(directory.Path, dir)), dir, ComparisonResult.DifferenceType.SourceMissing, false);
                        sendEvent(compareResult);
                        results.Add(compareResult);
                    }
                }
            }

            if (mCancelRequested)
            {
                progress.Canceled = true;
            }
            progress.Completed = true;
            return(results);
        }