显示带有复制文件夹过程的进度条 [英] Show Progress Bar with Copy Folders process

查看:105
本文介绍了显示带有复制文件夹过程的进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个将文件和嵌套文件夹复制到另一个位置的代码,我想显示进度条.我该怎么做?????????????任何建议/想法,或者有人给我一段代码,以便我可以嵌入其中???
请参见下面的代码:

Hi,

I have a code that copy files and nested folder to another location and i want to show the progress bar with that. How can i do that?????????????Any suggestion/idea or if somebody give me piece of code so that may be i''ll embed with it????
Please see the Code is shown below:

class CopyDir
   {
       public static void Copy(string sourceDirectory, string targetDirectory)
       {
           DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
           DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);

           CopyAll(diSource, diTarget);
       }

       public static void CopyAll(DirectoryInfo source, DirectoryInfo target)
       {

           // Check if the target directory exists, if not, create it.
           if (Directory.Exists(target.FullName) == false)
           {
               Directory.CreateDirectory(target.FullName);
           }

           // Copy each file into it's new directory.
           foreach (FileInfo fi in source.GetFiles())
           {
               //Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
               fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);

           }

               // Copy each subdirectory using recursion.
               foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
               {

   DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);

                   CopyAll(diSourceSubDir, nextTargetSubDir);

               }
       }
   }

推荐答案

您最好的选择是显示已复制多少文件的进度.根据复制的数据量,这比要做的工作多得多.但是,如果这是您要执行的操作,则建议您在Google上搜索FileSystemWatcher对象,然后查看它是否不能执行您想要的操作.
Your best bet is to show progress in terms of how many files have been copied. It''s a lot more work than it''s worth to do it by amount of data copied. However, if that''s what you want to do, I recommend that you google the FileSystemWatcher object, and see if that won''t do what you want.


也可以方法是先枚举要复制的所有文件,然后对要复制的字节求和.每次复制文件后,获取复制的字节数(文件大小)并基于该字节更新进度条.
Also one approach is to enumerate all the files to be copied first and then sum the bytes you''re going to copy. After each file copy get the byte amount copied (file size) and update the progress bar based on that.


由于这有点不同,所以我添加了一个新的解决方案.事件处理可能类似于:
Since this is a bit different thing I added a new solution. The event handling could be something like:
public class ProgressEventArgs : System.EventArgs {

   public decimal Percentage { get; set; }

   public ProgressEventArgs(decimal percentage)
      : base() {
      this.Percentage = percentage;
   }
}

public static class CopyDir {

   public static event System.EventHandler<ProgressEventArgs> ProgressChanged;
   public static bool CopyAll() {

      // This would be the actual copy iteration and percentage calculation
      for (decimal counter= 1;counter <= 10;counter++) {
         if (ProgressChanged != null) {
            ProgressChanged(null, new ProgressEventArgs((counter/10)*100));
         }
      }

      return true;
   }
}


并在您的表格上为事件添加连线:


and on your form you wire the event:

public Form1() {
   InitializeComponent();

   CopyDir.ProgressChanged += new EventHandler<ProgressEventArgs>(CopyDir_ProgressChanged);
}

void CopyDir_ProgressChanged(object sender, ProgressEventArgs e) {
   this.progressBar1.Value = (int)e.Percentage;
}


这篇关于显示带有复制文件夹过程的进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆