【问题标题】:Report progress copy for large file WPF报告大文件 WPF 的进度副本
【发布时间】:2016-08-15 19:16:27
【问题描述】:

我终于找到了使用进度条向我显示复制文件进度的方法,它适用于小文件,但是当我尝试复制大文件时,只有在文件完成时我才能看到任何进度复制。

如何接收已复制大小的报告?

这是我的代码:

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

谢谢

【问题讨论】:

    标签: c# wpf


    【解决方案1】:

    所有东西都在

     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;
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      相关资源
      最近更新 更多