2012-02-16 33 views
1

我有一个if语句,用于检查目录是否存在,以及它是否将该文件夹复制到指定位置。使用数组复制文件的进度条C#

整个复制过程位于预先确定的文件夹位置阵列上,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 
+0

什么样的错误?请更具体一些。您的问题与错误有关吗?或者它是如何实施进度条的? – ja72 2012-02-16 16:18:08

+0

我的问题是如何实现它对不起:P现在我只是在backgroundWorker上阅读:P – cheeseman 2012-02-16 16:54:13

+0

下载vXCopy(C#2.0)的源代码并从实现中学习。更好的是,按原样使用它。 http://vxcopy.codeplex.com/ – ja72 2012-02-16 16:19:53

回答

2

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

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

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 
+0

谢谢,我不认为这是这么简单:P 我现在只需要了解如何让它在后台工作器上工作,所以UI看起来不像它被冻结了:P – cheeseman 2012-02-21 18:17:32

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(); 

} 
0

尝试 { 串[] 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(Directory.GetDirectories中的字符串dirPath(SourcePath,“*”, SearchOption.AllDirectories)) { progressBar1.Value ++; 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(); 

      } 

     }