2016-08-15 29 views
0

最終的には進捗バーを使用してコピーファイルへの進捗状況を表示しますが、小さなファイルでは問題ありませんが、大きなファイルをコピーしようとしているときは見えませんファイルのコピーが完了したときにのみ進行します。大きなファイル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!"); 
    } 
} 

あなたは、システムの機能や進捗状況を報告よりもを使用してファイル全体をコピーする

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

答えて

0

にすべてのものをありがとうございます。

単一ファイルのコピーの進捗状況を確認する場合は、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; 

     } 
    } 
} 
+0

のように見えますが、非常にあなたに感謝しますあなたのコード内

! – dadou

関連する問題