报告大型文件WPF的进度副本

时间:2016-08-15 10:11:46

标签: c# wpf

我终于找到了使用进度条向我展示复制文件进度的方法,它对于小文件工作正常但是当我试图复制大文件时我无法做到只有在文件完成复制时才能看到任何进度。

我如何收到已复制尺寸的报告?

这是我的代码:

        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!");
    }
}

谢谢

1 个答案:

答案 0 :(得分:0)

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

使用系统功能复制整个文件而不是报告进度。

如果您想查看复制单个文件的进度,您必须自己复制它,就像在this answer中一样。

你的代码中的

看起来像是

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;

        }
    }
}
相关问题