报告大文件WPF的进度副本

本文关键字:副本 WPF 文件 报告 | 更新日期: 2023-09-27 18:02:36

我终于找到了使用进度条显示我复制文件的进度的方法,它对小文件工作很好,但是当我试图复制大文件时,我无法看到任何进度,只有当文件完成复制时。

我怎样才能收到已复制大小的报告?

这是我的代码:

        private void btnCopy_Click(object sender, RoutedEventArgs e)
    {
        backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
        backgroundWorker1.WorkerReportsProgress = true;
        backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
        backgroundWorker1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker1_RunWorkerCompleted);
        backgroundWorker1.RunWorkerAsync();
    }
    private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
        string source = @"C:'mi";
        string dest = @"C:'test";
        // Copy from the current directory, include subdirectories.
        string destDirName = dest + "''" + source.Substring(source.LastIndexOf(@"'") + 1);
        if (!Directory.Exists(dest))
        {
            Directory.CreateDirectory(destDirName);
        }
        DirectoryCopy(source, destDirName, true);
    }
    private void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
    {
        double Size = 0;
        double totalsize = 0;
        double filepercent = 0;
        // Get the subdirectories for the specified directory.
        DirectoryInfo dir = new DirectoryInfo(sourceDirName);
        if (!dir.Exists)
        {
            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();
        int filecount = files.Count();
        int i = 1;
        foreach (FileInfo fi in files)
        {
            totalsize += fi.Length;
        }
        totalsize = Math.Round((totalsize / 1048576), 2);
        foreach (FileInfo file in files)
        {
            Size += Math.Round(((double)file.Length / 1048576), 2);
            filepercent = Size * 100 / totalsize;
            string temppath = System.IO.Path.Combine(destDirName, file.Name);
            file.CopyTo(temppath, false);
            backgroundWorker1.ReportProgress((int)filepercent,"Files " + i + "/" + filecount + " " + Size + "/" + totalsize + " Mb");
            Thread.Sleep(100);
            i++;
        }
        ////If copying subdirectories, copy them and their contents to new location.
        if (copySubDirs)
        {
            foreach (DirectoryInfo subdir in dirs)
            {
                string temppath = System.IO.Path.Combine(destDirName, subdir.Name);
                DirectoryCopy(subdir.FullName, temppath, copySubDirs);
            }
        }
    }
    private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        progressbar.Value = e.ProgressPercentage;
        lblpercent.Content = e.UserState as string;
    }
    private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        MessageBox.Show("All the files were copied!");
    }
}

谢谢

报告大文件WPF的进度副本

 file.CopyTo(temppath, false);
 backgroundWorker1.ReportProgress((int)filepercent,"Files " + i + "/" + filecount + " " + Size + "/" + totalsize + " Mb");
        Thread.Sleep(100);

你正在使用系统功能复制整个文件,然后报告进度。

如果你想看到复制单个文件的进度,你必须自己复制,就像这个答案一样。

在你的代码中它看起来像

int buflen = 1024;
byte[] buf = new byte[buflen];
long totalBytesRead = 0;
using (FileStream sourceStream = 
          new FileStream(file.FullName, FileMode.Open))
{
    using (FileStream destStream = 
                new FileStream(tempPath, FileMode.CreateNew))
    {
        while (true)
        {
            numReads++;
            int bytesRead = sourceStream.Read(buf, 0, buflen);
            if (bytesRead == 0) break; 
            destStream.Write(buf, 0, bytesRead);
            totalBytesRead += bytesRead;
            // TODO: Here you can track your progress
            // backgroundWorker1.ReportProgress((int)filepercent,"Files " + i + "/" + filecount + " " + Size + "/" + totalsize + " Mb");        
            if (bytesRead < buflen) break;
        }
    }
}