Пример #1
0
        public static void Create(String path, NameConflictOption aNameConflictOption)
        {
            if (File.Exists(path))
            {
                switch (aNameConflictOption)
                {
                case NameConflictOption.Cancel:
                case NameConflictOption.Skip:
                    return;

                case NameConflictOption.Overwrite:
                    FileExt.Delete(path);
                    break;

                case NameConflictOption.RenameExisting:
                    FileExt.Move(path, FileExt.MakeUnique(path));
                    break;

                case NameConflictOption.RenameNew:
                    throw new InvalidDataException("Can't rename a file we are trying to create");

                default:
                    break;
                }
            }
            DirectoryExt.Create(Path.GetDirectoryName(path));
            FileStream stream = File.Create(path);

            stream.Close();
        }
Пример #2
0
        /// <summary>
        /// Copies a directory.
        /// </summary>
        /// <param name="aSource">The source location.</param>
        /// <param name="aDestination">The destination location.</param>
        /// <param name="aSearchOption">If sub directories should be copied.</param>
        /// <param name="aConflictOption">How to handle name conflicts.</param>
        public static void Copy(String aSource, String aDestination, DirectorySearchOption aSearchOption, NameConflictOption aConflictOption)
        {
            if (Directory.Exists(aSource))
            {
                if (!Directory.Exists(aDestination))
                {
                    Directory.CreateDirectory(aDestination);
                }
                //Now Create all of the directories
                foreach (string dirPath in DirectoryExt.GetDirectories(aSource, aSearchOption))
                {
                    Create(dirPath.Replace(aSource.endDir(), aDestination.endDir()));
                }

                //Copy all the files
                foreach (string copyFrom in DirectoryExt.GetFiles(aSource, aSearchOption))
                {
                    String copyTo = copyFrom.Replace(aSource.endDir(), aDestination.endDir());
                    if (File.Exists(copyTo))
                    {
                        if (aConflictOption == NameConflictOption.Cancel)
                        {
                            return;
                        }
                        else if (aConflictOption == NameConflictOption.Skip)
                        {
                            continue;
                        }
                    }
                    FileExt.Copy(copyFrom, copyTo, aConflictOption);
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Delete all files in the given directory that match the provided pattern.
        /// </summary>
        /// <param name="aPath">The directory where the files should be found.</param>
        /// <param name="aSearchOption">The directory search options.</param>
        /// <returns>If the deletion was successful.</returns>
        public static bool DeleteFiles(String aPath, DirectorySearchOption aSearchOption)
        {
            bool success = true;

            if (Directory.Exists(aPath))
            {
                success = FileExt.Delete(DirectoryExt.GetFiles(aPath, aSearchOption));
            }
            return(success);
        }
Пример #4
0
        /// <summary>
        /// Moves a directory from one location to another.
        /// </summary>
        /// <param name="aSource">The source location for the move.</param>
        /// <param name="aDestination">The destination location for the move.</param>
        /// <param name="aNameConflictOption">How to handle name conflicts</param>
        /// <returns>If the move was successful.</returns>
        public static bool Move(String aSource, String aDestination, NameConflictOption aNameConflictOption)
        {
            if (!Directory.Exists(aSource))
            {
                throw new FileNotFoundException("The source directory does not exist");
            }
            bool success = true;

            if (Directory.Exists(aSource))
            {
                success = false;

                if (Directory.Exists(aDestination))
                {
                    switch (aNameConflictOption)
                    {
                    case NameConflictOption.Skip:
                    case NameConflictOption.Cancel:
                        return(false);

                    case NameConflictOption.Overwrite:
                        DirectoryExt.Delete(aDestination);
                        break;

                    case NameConflictOption.RenameExisting:
                        DirectoryExt.Move(aDestination, DirectoryExt.MakeUnique(aDestination), NameConflictOption.RenameNew);
                        break;

                    case NameConflictOption.RenameNew:
                        aDestination = DirectoryExt.MakeUnique(aDestination);
                        break;

                    default:
                        throw new ArgumentException("Invalid conflict option.");
                    }
                }

                int tries = 0;
                while (!success && tries < Constants.STANDARD_RETRY_ATTEMPTS)
                {
                    try
                    {
                        Directory.Move(aSource, aDestination);
                        success = true;
                    }
                    catch
                    {
                        tries++;
                        System.Threading.Thread.Sleep(Constants.STANDARD_RETRY_SLEEP);
                    }
                }
            }
            return(success);
        }
Пример #5
0
        /// <summary>
        /// Gets the first file in the directory that matches the search options.
        /// </summary>
        /// <param name="aPath">The directory to search.</param>
        /// <param name="aSearchOption">The options to use when searching.</param>
        /// <param name="aFile">The path to the first file or an empty string if no files are found.</param>
        /// <returns>True if a file was found.</returns>
        public static bool GetFirstFile(String aPath, DirectorySearchOption aSearchOption, out String aFile)
        {
            aFile = String.Empty;
            List <String> files = DirectoryExt.GetFiles(aPath, aSearchOption);

            if (!files.isNull())
            {
                foreach (String file in files)
                {
                    aFile = file;
                    return(true);
                }
            }
            return(false);
        }
Пример #6
0
        public static void Copy(String source, String destination, NameConflictOption aNameConflictOption, Action <ProcessProgress> handler, ref int cancel)
        {
            if (!File.Exists(source))
            {
                throw new FileNotFoundException("The source file could not be found.");
            }
            if (File.Exists(destination))
            {
                switch (aNameConflictOption)
                {
                case NameConflictOption.Cancel:
                case NameConflictOption.Skip:
                    return;

                case NameConflictOption.Overwrite:
                    Delete(destination);
                    break;

                case NameConflictOption.RenameExisting:
                    FileExt.Move(destination, FileExt.MakeUnique(destination));
                    break;

                case NameConflictOption.RenameNew:
                    destination = FileExt.MakeUnique(destination);
                    break;

                default:
                    break;
                }
            }

            if (!Directory.Exists(Path.GetDirectoryName(destination)))
            {
                DirectoryExt.Create(Path.GetDirectoryName(destination));
            }

            FileInfo fileInfo = new FileInfo(source);
            bool     noBuffer = fileInfo.Length < Constants.USE_BUFFER_FILE_SIZE_BYTES;

            XCopy.Copy(source, destination, aNameConflictOption == NameConflictOption.Overwrite, noBuffer, handler, ref cancel);
        }
Пример #7
0
 public static void Write(String filename, String text, bool append)
 {
     //Clear readonly flag
     if (File.Exists(filename))
     {
         File.SetAttributes(filename, FileAttributes.Normal);
     }
     if (append)
     {
         File.AppendAllText(filename, text);
     }
     else
     {
         if (File.Exists(filename))
         {
             Delete(filename);
         }
         if (!Directory.Exists(Path.GetDirectoryName(filename)))
         {
             DirectoryExt.Create(Path.GetDirectoryName(filename));
         }
         File.WriteAllText(filename, text);
     }
 }
Пример #8
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);
        }