关于加快硬盘备份代码的建议

时间:2018-12-23 16:47:45

标签: c# file-io

我有以下硬盘驱动器备份代码,用于比较每个文件在复制前的.LastWriteTime()时间,并且运行速度比我预期的要慢。我的假设是,如果没有要更新的文件,它应该运行得很快(大约几分钟)。我发现通过USB3.0的210 GB仍需要一个多小时。我想知道我的代码中是否有任何不必要的,耗时的部分可以改进。我也在考虑将每个directorycopy()调用放在不同的线程上(至少对于目录的第一级来说,但是不确定这是否是不好的做法)。

该代码主要是借来的:

https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories

我进行了更改,以忽略$ Recycle Bin文件夹,记录已更改的文件或存在诸如长文件名和故意处理异常的问题之类的文件。但最重要的是,我添加了一项检查,以便在复制之前查看哪个文件较新。

    private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
    {
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        if (sourceDirName.Contains("$")) // avoids $Recycle Bin
            return;

        if (!dir.Exists)
        {
            textb_Status.AppendText("Issue with " + dir.FullName + " This folder will not be compied.");
            return;
            //throw new DirectoryNotFoundException(
               // "Source directory does not exist or could not be found: "
              //  + sourceDirName);
        }

        DirectoryInfo[] dirs = dir.GetDirectories();
        // If the destination directory doesn't exist, create it.
        if (!Directory.Exists(destDirName))
        {
            Directory.CreateDirectory(destDirName);
        }

        // Get the files in the directory and copy them to the new location.
        FileInfo[] files = dir.GetFiles();
        foreach (FileInfo file in files)
        {
            string temppath = Path.Combine(destDirName, file.Name);
            try
            {
                file.CopyTo(temppath);
            }
            catch (PathTooLongException)
            {
                textb_Status.AppendText("Filename Too long \n " + file.FullName + "\n");
            }
            catch (IOException ex)
            {
                FileInfo sourcefile = new FileInfo(file.FullName);
                FileInfo destFile = new FileInfo(temppath);
                int CompareValue = sourcefile.LastWriteTime.CompareTo(destFile.LastWriteTime); //<0==> Earlier (old)  =0 ==> same  >0 Later (newer)
                //textb_Status.AppendText("CompareValue: " + CompareValue + "\n");
                if (CompareValue > 0) // Represents newer file
                {
                    file.CopyTo(temppath, true);
                    textb_Status.AppendText("Updated: " + file.FullName + "\n");
                }
            }

            catch (Exception ex2)
            {
                textb_Status.AppendText("Issue with " + file.FullName + "\n");
                textb_Status.AppendText("Error Message \n");
                textb_Status.AppendText(ex2.Message + "\n");
            }

        }

        // If copying subdirectories, copy them and their contents to new location.
        if (copySubDirs)
        {
            foreach (DirectoryInfo subdir in dirs)
            {
                string temppath = Path.Combine(destDirName, subdir.Name);
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }

如果只有几个文件要更新,我希望备份过程在几分钟左右。

2 个答案:

答案 0 :(得分:0)

我不认为这是数据量,它会减慢该过程的速度,但会减少文件的数量。不管文件大小如何,初始文件访问(检查它是否存在,获取统计信息)都非常昂贵。此外,许多人认为将异常用于控制流不良样式,并且抛出和捕获异常可能非常昂贵。在您的用例中(即大多数文件未更改),抛出了许多异常。

根据磁盘(SSD或HDD)的不同,多线程读写可能不是一个好主意,并且会减慢整个过程的速度。

根据File.Copy()的实现,您可能会更好,先检查目标,然后在确实需要时才执行Copy。但这是基准测试之后您才知道的。

答案 1 :(得分:0)

谢谢@derpirscher。对于每个文件,我在尝试写入之前都会检查它是否存在。这样,不会引发异常。在大约5秒钟内检查了驱动器!我在源目录的深处修改了一些文件,以确保已检测到并复制了它们。他们是。

我觉得例外很昂贵,我只是不知道这是不好的。...很棒的教训!

我的代码如下。注意:尝试从“系统卷信息”文件夹中获取文件时收到错误消息,因此从检查开始,以确保sourceDirName不等于该目录。

    private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
    {
        if (sourceDirName.Contains("System Volume Information"))
            return;
        //textb_Status.AppendText("Current Directory: " + sourceDirName +"\n");
        DirectoryInfo[] dirs = null;
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        if (sourceDirName.Contains("$")) // avoids $Recycle Bin
            return;

        if (!dir.Exists)
        {
            textb_Status.AppendText("Issue with " + dir.FullName + " This folder will not be compied.");
            return;
            //throw new DirectoryNotFoundException(
            // "Source directory does not exist or could not be found: "
            //  + sourceDirName);
        }

        {

            dirs = dir.GetDirectories();

            // If the destination directory doesn't exist, create it.
            if (!Directory.Exists(destDirName))
            {
                Directory.CreateDirectory(destDirName);
            }

            // Get the files in the directory and copy them to the new location.
            FileInfo[] files = dir.GetFiles();
            foreach (FileInfo file in files)
            {
                string temppath = Path.Combine(destDirName, file.Name);
                try
                {
                    if (File.Exists(temppath)) // Check for newer
                    {
                        FileInfo sourcefile = new FileInfo(file.FullName);
                        FileInfo destFile = new FileInfo(temppath);
                        int CompareValue = sourcefile.LastWriteTime.CompareTo(destFile.LastWriteTime); //<0==> Earlier (old)  =0 ==> same  >0 Later (newer)
                                                                                                       //textb_Status.AppendText("CompareValue: " + CompareValue + "\n");
                        if (CompareValue > 0) // Represents newer file
                        {
                            file.CopyTo(temppath, true);
                            textb_Status.AppendText("********** Updated: " + file.FullName + "********* \n");
                        }
                    }
                    else
                    {
                        file.CopyTo(temppath);
                    }

                }
                catch (PathTooLongException)
                {
                    textb_Status.AppendText("Filename Too long \r\n\n " + file.FullName + "\r\n\n");
                }
                catch (IOException ex)
                {
                    FileInfo sourcefile = new FileInfo(file.FullName);
                    FileInfo destFile = new FileInfo(temppath);
                    int CompareValue = sourcefile.LastWriteTime.CompareTo(destFile.LastWriteTime); //<0==> Earlier (old)  =0 ==> same  >0 Later (newer)
                                                                                                   //textb_Status.AppendText("CompareValue: " + CompareValue + "\n");
                    if (CompareValue > 0) // Represents newer file
                    {
                        file.CopyTo(temppath, true);
                        textb_Status.AppendText("Updated: " + file.FullName + "\n");
                    }
                }

                catch (Exception ex2)
                {
                    textb_Status.AppendText("Issue with " + file.FullName + "\n");
                    textb_Status.AppendText("Error Message \n");
                    textb_Status.AppendText(ex2.Message + "\n\n");
                }
            }


            // If copying subdirectories, copy them and their contents to new location.
            if (copySubDirs)
            {
                foreach (DirectoryInfo subdir in dirs)
                {
                    string temppath = Path.Combine(destDirName, subdir.Name);
                    DirectoryCopy(subdir.FullName, temppath, copySubDirs);
                }

            }
        }
    }