【问题标题】:Progress Bar for Copying Files using an Array C#使用数组 C# 复制文件的进度条
【发布时间】:2012-03-08 00:36:16
【问题描述】:

我有一个 if 语句来检查一个目录是否存在,如果存在,它会将该文件夹复制到指定位置。

整个复制过程是在一组预先确定的文件夹位置上进行的,for 循环遍历该数组并在每个位置复制文件夹及其数据。

目前有 200 个不同的位置可供复制,还有更多位置待添加。

我正在尝试围绕复制这 200 多个文件夹实现一个进度条,但一直遇到错误,我认为我遇到的问题主要是由于数组,我看过的教程(差异很大彼此)只涵盖了基本的文件复制。

任何有关如何使进度条工作的帮助或提示将不胜感激:)

for (int i = 0; i < pathArray.Length; i++)
{

   string sourcePath = pathArray[i];

   //MISSING CODE

   if (System.IO.Directory.Exists(sourcePath))
   {                   

      System.IO.Directory.CreateDirectory(targetPathProper);

      foreach (string dirPath in System.IO.Directory.GetDirectories(sourcePath,"*",
          (System.IO.SearchOption.AllDirectories)))
      {
         System.IO.Directory.CreateDirectory(dirPath.Replace(sourcePath, 
                targetPathProper));
      }

      foreach (string newPath in System.IO.Directory.GetFiles(sourcePath, "*", 
          (System.IO.SearchOption.AllDirectories)))
      {
         System.IO.File.Copy(newPath, newPath.Replace(sourcePath, 
             targetPathProper), true);
      }

   } //end if
} // end for

【问题讨论】:

  • 什么样的错误?请更具体。您的问题是否与错误有关,或者它如何实现进度条?
  • 下载vXCopy (C# 2.0) 的源码,学习实现。更好的是,按原样使用它。 vxcopy.codeplex.com
  • 我的问题是如何实现它对不起:P 目前我只是在阅读 backgroundWorker :P

标签: c# arrays winforms progress-bar


【解决方案1】:

你说你得到一个错误,你得到什么错误?

至于您的进度条,您可以简单地在数组中的每个基本目录处递增。没有真正需要为每个文件增加。或者,如果需要指示每个文件的进度,您可以有两个进度条。

progressBar1.Maximum = pathArray.Length;
progressBar1.Value = 0;
for (int i = 0; i < pathArray.Length; i++)
{

   string sourcePath = pathArray[i];

   progressBar1.Value++;

   if (System.IO.Directory.Exists(sourcePath))
   {                   

      System.IO.Directory.CreateDirectory(targetPathProper);
      string[] subDirs = System.IO.Directory.GetDirectories(sourcePath,"*",(System.IO.SearchOption.AllDirectories))
      progressBar2.Maximum = subDirs.Length;
      progressBar2.Value = 0;
      foreach (string dirPath in subDirs)
      {
         progressBar2.Value++;
         System.IO.Directory.CreateDirectory(dirPath.Replace(sourcePath, 
            targetPathProper));
         Application.DoEvents();
      }

      progressBar2.Value = 0;
      foreach (string newPath in subDirs)
      {
         progressBar2.Value++;
         System.IO.File.Copy(newPath, newPath.Replace(sourcePath, 
             targetPathProper), true);
         Application.DoEvents();
      }

   } //end if
} // end for

【讨论】:

  • 谢谢,我没想到这么简单:PI 现在只需要阅读如何让它在后台工作人员上工作,这样 UI 就不会看起来像被冻结了:P跨度>
【解决方案2】:
string[] subDirs = 
            System.IO.Directory.GetDirectories(SourcePath, "*",
            (System.IO.SearchOption.AllDirectories));
                int subFiles = System.IO.Directory.GetFiles(SourcePath, "*.*",
         SearchOption.AllDirectories).Length;
 progressBar1.Maximum = subFiles+(subDirs.Length);
 progressBar1.Value = 0;
 foreach (string dirPath in Directory.GetDirectories(SourcePath, "*",
       SearchOption.AllDirectories))
 {
       progressBar1.Value++;
       progressBar1.PerformStep();
       Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath));
       Application.DoEvents();   

 }

 foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
                 SearchOption.AllDirectories))
 {
       progressBar1.Value++;
       File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);
       Application.DoEvents();

 }

【讨论】:

    【解决方案3】:

    您可以使用 Backgroundworker,但如果您想使用其他方法 这是一个包含可下载源代码的链接,它也有助于您入门,因为我假设这是一个 winforms 应用程序

    How to copy files in C# with a customizable progress indicator and or progress bar

    【讨论】:

      【解决方案4】:

      试试 { string[] subDirs = System.IO.Directory.GetDirectories(SourcePath, "", (System.IO.SearchOption.AllDirectories)); int subFiles = System.IO.Directory.GetFiles(SourcePath, ".*", SearchOption.AllDirectories).Length; progressBar1.Maximum = subFiles+(subDirs.Length); 进度条1.Value = 0; foreach(Directory.GetDirectories 中的字符串 dirPath(SourcePath,“*”, SearchOption.AllDirectories)) { 进度条1.值++; Directory.CreateDirectory(dirPath.Replace(SourcePath, DestinationPath)); 应用程序.DoEvents();
      }

                  foreach (string newPath in Directory.GetFiles(SourcePath, "*.*",
                       SearchOption.AllDirectories))
                  {
                      progressBar1.Value++;
                      File.Copy(newPath, newPath.Replace(SourcePath, DestinationPath), true);
                      Application.DoEvents();
      
                  }
      
              }
      

      【讨论】:

        猜你喜欢
        • 2015-12-12
        • 2012-12-23
        • 1970-01-01
        • 2014-01-13
        • 1970-01-01
        • 2011-08-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多