Exemplo n.º 1
0
        public static void GetDirectoryFileSize(string sourceDirName, bool countSubDirs, BackupSizeAndProgress backupSizeAndProgress)
        {
            DirectoryInfo sourceDir;
            DirectoryInfo[] sourceDirArray;
            FileInfo[] files;

            try
            {
                sourceDir = new DirectoryInfo(sourceDirName);
                sourceDirArray = sourceDir.GetDirectories();

                if (!sourceDir.Exists)
                {
                    return;
                }

                files = sourceDir.GetFiles();
            }
            catch (Exception)
            {
                return;
            }

            foreach (FileInfo file in files)
            {
                try
                {
                    if (backupSizeAndProgress.ExcludedExtensions.Contains(file.Extension))
                    {
                        backupSizeAndProgress.TotalFilesToExclude++;
                        continue;
                    }
                    backupSizeAndProgress.TotalFileToBackup++;
                    backupSizeAndProgress.TotalSizeToBackup += file.Length;
                }
                catch (Exception)
                {
                    return;
                }
            }

            if (countSubDirs)
            {
                foreach (DirectoryInfo subdir in sourceDirArray)
                {
                    try
                    {
                        backupSizeAndProgress.TotalDirectoryToBackup++;
                        GetDirectoryFileSize(subdir.FullName, countSubDirs, backupSizeAndProgress);
                    }
                    catch (Exception)
                    {
                        return;
                    }
                }
            }
        }
Exemplo n.º 2
0
        public static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs, string tooLongFolder, LoggingLog backupLog,
            bool showErrorsOnScreen, BackupSizeAndProgress backupSizeAndProgress)
        {
            DirectoryInfo sourceDir;
            DirectoryInfo[] sourceDirArray;
            try
            {
                sourceDir = new DirectoryInfo(sourceDirName);
                sourceDirArray = sourceDir.GetDirectories();
            }
            catch (UnauthorizedAccessException)
            {
                //this is for hidden system files
                return;
            }
            catch (Exception ex)
            {
                backupLog.LogAndPrintScreenMessage(String.Format("Could not copy source directory: {0}. Because: {1}", sourceDirName, ex), showErrorsOnScreen);
                backupSizeAndProgress.HadIssues = true;
                return;
            }

            // If the source directory does not exist
            if (!sourceDir.Exists)
            {
                backupLog.LogAndPrintScreenMessage(String.Format("Could not copy source directory: {0}. Source directory does not exist or could not be found.", sourceDirName), showErrorsOnScreen);
                backupSizeAndProgress.HadIssues = true;
                return;
            }

            // If the destination directory does not exist, create it.
            try
            {
                if (!Directory.Exists(destDirName))
                {
                    Directory.CreateDirectory(destDirName);
                }
            }
            catch (PathTooLongException)
            {
                DirectoryInfo dirInfo = sourceDir;
                string dirName = dirInfo.Name;
                string newDestName = String.Format("{0}_TooLong_{1}", dirName, DateTime.Now.ToString("hhmmssffff"));
                string newDestPath = Path.Combine(tooLongFolder, newDestName);

                DirectoryCopy(sourceDirName, newDestPath, copySubDirs, tooLongFolder, backupLog, showErrorsOnScreen, backupSizeAndProgress);
                return;
            }
            catch (Exception ex)
            {
                //TODO: figure out what exception still caught
                backupLog.LogAndPrintScreenMessage(String.Format("Could not create destination directory: {0}. Because: {1}", destDirName, ex), showErrorsOnScreen);
                backupSizeAndProgress.HadIssues = true;
                return;
            }

            // Get the file contents of the directory to copy.
            FileInfo[] files = sourceDir.GetFiles();

            foreach (FileInfo file in files)
            {
                // Create the path to the new copy of the file.
                string temppath = Path.Combine(destDirName, file.Name);

                // Copy the file.
                try
                {
                    if (backupSizeAndProgress.ExcludedExtensions.Contains(file.Extension))
                    {
                        backupSizeAndProgress.TotalFilesExcluded++;
                        continue;
                    }
                    file.CopyTo(temppath, true);
                    backupSizeAndProgress.TotalSizeBackedup += file.Length;
                    backupSizeAndProgress.TotalFileBackedup++;
                }
                catch (PathTooLongException)
                {
                    string parentDirName;
                    try
                    {
                        DirectoryInfo dirInfo = file.Directory;
                        parentDirName = dirInfo.Name;
                    }
                    catch (PathTooLongException)
                    {
                        parentDirName = "NoDirectory";
                    }

                    string fullDestPath = "Not Assigned";
                    try
                    {
                        string newDestName = String.Format("{0}_TooLong_{1}", parentDirName, DateTime.Now.ToString("hhmmssffff"));
                        string newDestPath = Path.Combine(tooLongFolder, newDestName);
                        Directory.CreateDirectory(newDestPath);
                        fullDestPath = Path.Combine(newDestPath, file.Name);
                        file.CopyTo(fullDestPath, true);

                        backupSizeAndProgress.TotalSizeBackedup += file.Length;
                        backupSizeAndProgress.TotalFileBackedup++;
                    }
                    catch (Exception ex)
                    {
                        backupLog.LogAndPrintScreenMessage(String.Format("Failed to copy Too Long file twice: {0} to Destination: {1}, Because: {2}", file.Name, fullDestPath, ex.Message), showErrorsOnScreen);
                    }
                }
                catch (Exception ex)
                {
                    backupLog.LogAndPrintScreenMessage(String.Format("Could not copy: {0}. Because: {1}", file.FullName, ex), showErrorsOnScreen);
                    backupSizeAndProgress.HadIssues = true;
                }
            }

            // If copySubDirs is true, copy the subdirectories.
            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in sourceDirArray)
                {
                    // Create the subdirectory.
                    string temppath = Path.Combine(destDirName, subdir.Name);

                    // Copy the subdirectories.
                    backupSizeAndProgress.TotalDirectoryBackedup++;
                    DirectoryCopy(subdir.FullName, temppath, true, tooLongFolder, backupLog, showErrorsOnScreen, backupSizeAndProgress);
                }
            }
        }