Exemple #1
0
        public static void RecursivelyCopyFolders(DirectoryInfo FromFolder, DirectoryInfo ToFolder, KlerksSoft.EasyProgressDialog.ProgressDialog progressDialog)
        {
            //Simple recursive copy.
            // - Does not delete additional files from destination
            // - Does not copy files that already exist with similar modified date (within 5 secs)

            foreach (FileInfo sourceFile in FromFolder.GetFiles())
            {
                bool skipFile = false;

                if (File.Exists(Path.Combine(ToFolder.FullName, sourceFile.Name)))
                {
                    FileInfo targetFile = new FileInfo(Path.Combine(ToFolder.FullName, sourceFile.Name));
                    if (targetFile.LastWriteTime.AddSeconds(5) > sourceFile.LastWriteTime &&
                        targetFile.LastWriteTime.AddSeconds(-5) < sourceFile.LastWriteTime &&
                        targetFile.Length == sourceFile.Length
                        )
                    {
                        skipFile = true;
                    }
                }

                progressDialog.Worker_SetSpecificProgress(null, progressDialog.CurrentCount + sourceFile.Length, null);

                if (!skipFile)
                {
                    sourceFile.CopyTo(Path.Combine(ToFolder.FullName, sourceFile.Name), true);
                }
            }

            foreach (DirectoryInfo sourceFolder in FromFolder.GetDirectories())
            {
                DirectoryInfo targetFolder;

                if (Directory.Exists(Path.Combine(ToFolder.FullName, sourceFolder.Name)))
                {
                    targetFolder = new DirectoryInfo(Path.Combine(ToFolder.FullName, sourceFolder.Name));
                }
                else
                {
                    targetFolder = Directory.CreateDirectory(Path.Combine(ToFolder.FullName, sourceFolder.Name));
                }

                RecursivelyCopyFolders(sourceFolder, targetFolder, progressDialog);
            }
        }
        private bool RecursiveDatabaseBasedCopy(DirectoryInfo fromFolder, int backupID)
        {
            foreach (FileInfo sourceFile in fromFolder.GetFiles())
            {
                bool   physicalCopy;
                string physicalFileName;

                int?existingFileID = LoadedBackupDB.GetFileRecordIDByProperties(_source.StripSourceFromPath(sourceFile.FullName), sourceFile.LastWriteTime, sourceFile.Length);

                if (existingFileID == null)
                {
                    existingFileID   = LoadedBackupDB.AddFileRecord(_source.StripSourceFromPath(sourceFile.FullName), sourceFile.LastWriteTime, sourceFile.Length);
                    physicalFileName = "";
                    physicalCopy     = true;
                }
                else
                {
                    physicalFileName = LoadedBackupDB.GetBackupFilePathByEntryID(existingFileID.Value);

                    if (!string.IsNullOrEmpty(physicalFileName) && File.Exists(Path.Combine(FullBackupLocationDBDataPath, physicalFileName)))
                    {
                        physicalCopy = false;
                    }
                    else
                    {
                        physicalCopy = true;
                    }
                }

                if (physicalCopy)
                {
                    physicalFileName = DefineDBDataFileName(existingFileID.Value, _source.StripSourceFromPath(sourceFile.FullName), sourceFile.LastWriteTime, sourceFile.Length, _password);

                    string SHA1 = "";
                    if (string.IsNullOrEmpty(_password))
                    {
                        sourceFile.CopyTo(Path.Combine(FullBackupLocationDBDataPath, physicalFileName), true);
                        //No SHA1 - would be expensive to calculate given that we are using simple file copy.
                    }
                    else
                    {
                        SHA1 = Utils.EncryptFile(sourceFile.FullName, Path.Combine(FullBackupLocationDBDataPath, physicalFileName), _password);
                    }

                    LoadedBackupDB.UpdateDBDataFileName(existingFileID.Value, physicalFileName, SHA1);
                }

                LoadedBackupDB.AddBackupFileRecord(backupID, existingFileID.Value);

                //update progress (exit if cancelled)
                if (_progressDialog != null && !_progressDialog.Worker_SetSpecificProgress(null, _progressDialog.CurrentCount + sourceFile.Length, null))
                {
                    return(false);
                }
            }

            foreach (DirectoryInfo sourceFolder in fromFolder.GetDirectories())
            {
                if (!RecursiveDatabaseBasedCopy(sourceFolder, backupID))
                {
                    return(false); //if cancelled at any point, return/abort. (recursive...)
                }
            }

            return(true); //if we got to this point, so far so good! (recursive...)
        }