简体   繁体   English

使用字节复制文件+进度条

[英]Copy Files + progress bar using bytes

I'm trying to put a progress on my file copy form, got that to work but the progress bar updates based on the number of files. 我正在尝试在我的文件复制表单上取得进展,让它工作但进度条根据文件数量更新。 Problem is that there are a lot of small files, and some really big ones that need to be copied. 问题是有很多小文件,还有一些非常大的文件需要复制。

So I'd like to know if instead of using the file number, if there's a way to check the number of bytes being written instead. 所以我想知道是否有一种方法来检查写入的字节数而不是使用文件号。 Any suggestions are much appreciated. 任何建议都非常感谢。 Here is the code I have now: 这是我现在的代码:

    private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
    {
        string SourcePath = RegistryRead.ReadOriginalPath();
        string DestinationPath = RegistryRead.ReadNewPath();

        if (Directory.Exists(SourcePath))
        {
            //Now Create all of the directories 
            string[] allDirectories = Directory.GetDirectories(SourcePath, "*", SearchOption.AllDirectories);
            string[] allFiles = Directory.GetFiles(SourcePath, "*.*", SearchOption.AllDirectories);
            int numberOfItems = allDirectories.Length + allFiles.Length;
            int progress = 0;

            foreach (string dirPath in allDirectories)
            {
                Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
                progress++;
                BackgroundWorker.ReportProgress(100 * progress / numberOfItems);
            }

            //Copy all the files 
            foreach (string newPath in allFiles)
            {
                File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath));
                progress++;
                BackgroundWorker.ReportProgress(100 * progress / numberOfItems);
            }
        }

    }

    private void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        // Change the value of the ProgressBar to the BackgroundWorker progress.
        progressBar1.Value = e.ProgressPercentage;
    }

Thanks in advance 提前致谢

If you want to do this yourself you can 如果你想自己做,你可以

  1. Iterate over all files counting the total size to be copied. 迭代计算要复制的总大小的所有文件。
  2. Call CopyFileEx to do the copying. 调用CopyFileEx进行复制。 This uses a callback mechanism to report progress and that callback includes the number of bytes copied for the current file. 这使用回调机制来报告进度,并且回调包括为当前文件复制的字节数。
  3. Keep track of how many bytes have been copied in total and use that to report an overall progress. 跟踪总共已复制的字节数,并使用它来报告总体进度。

There is quite possibly a .net managed equivalent to CopyFileEx. 很可能.net管理等同于CopyFileEx。

However, I would recommend calling SHFileOperation and let the system do the hard work for you. 但是,我建议调用SHFileOperation并让系统为您努力工作。 As you are discovering this task is exceedingly hard to do well. 当你发现这个任务非常难以做好。 As an added benefit of using the shell to do the work, you'll get the standard system dialog. 作为使用shell完成工作的额外好处,您将获得标准系统对话框。

You will need to implement your own file copying code, and report the progress from there. 您需要实现自己的文件复制代码,并从那里报告进度。

EDIT: In case you don't want to copy the files yourself, you could start another background worker that will check the file copy progress (get the properties of the currently copied file, and check its length). 编辑:如果您不想自己复制文件,可以启动另一个后台工作程序来检查文件复制进度(获取当前复制文件的属性,并检查其长度)。 If you don't run into sharing issues, it will provide you with the information you need to update the progress bar. 如果您没有遇到共享问题,它将为您提供更新进度条所需的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM