コード例 #1
0
        private static int OverwriteModifiedFiles(FolderDiffBase folderDiff, ProgressReporter progressReporter, int reportCount)
        {
            // Replace target files that have been edited in source with their new version
            ProgressReporter.Logger.WriteLine("Overwritting " + folderDiff.ModifiedFiles.Length + " modified files");

            int consoleTh  = folderDiff.ModifiedFiles.Length / reportCount;
            int k          = 0;
            int errorCount = 0;

            Parallel.For(0, folderDiff.ModifiedFiles.Length, new ParallelOptions()
            {
                MaxDegreeOfParallelism = Program.MaxDegreeOfParallelism
            }, i =>
            {
                string sourcePath = Path.Combine(folderDiff.SourcePath, folderDiff.ModifiedFiles[i]);
                string targetPath = Path.Combine(folderDiff.TargetPath, folderDiff.ModifiedFiles[i]);

                // read https://stackoverflow.com/questions/265953/how-can-you-easily-check-if-access-is-denied-for-a-file-in-net
                try
                {
                    File.Copy(sourcePath, targetPath, true);
                }
                catch (Exception e)
                {
                    Interlocked.Increment(ref errorCount);
                    ProgressReporter.Logger.WriteLine("\r\nERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR\r\nERROR : " + e.ToString() + "\r\nERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR ERROR\r\n");
                }

                progressReporter.AddModifiedFileProgress(i);

                int progress = Interlocked.Increment(ref k);
                progressReporter.WriteDetailedModifiedFileReport(sourcePath, targetPath, i, LogLevel.File);
                if (consoleTh == 0 || progress % consoleTh == 0)
                {
                    progressReporter.WriteReport(LogLevel.Console);
                }
            });
            progressReporter.WriteReport(LogLevel.Console | LogLevel.File);
            return(errorCount);
        }