复制一个目录的进度条包括Windows窗体应用程序中的所有子目录和文件

时间:2016-01-02 08:07:37

标签: winforms progress-bar

我将一个驱动器中存在的一个目录(包括所有子目录和文件)复制到另一个具有相同名称的驱动器中,我希望显示一个进度条我已经看到了很多带有计时器的例子但是我没有知道需要花多少时间,所以请指导我如何实现

我写了这样的代码

 private void btnDriveSubmit_Click(object sender, EventArgs e)
    {          
        ErrorProvider errorproviderDrive = new ErrorProvider();
        if (!String.IsNullOrEmpty(tbDrive.Text))
        {
            if (tbDrive.TextLength == 1)
            {
                drive = tbDrive.Text;
                string org1 = Application.StartupPath + "\\UserFirstDetails.xml";
                UsrDetails.Load(org1);
                XmlNode drivetag = UsrDetails.SelectSingleNode("UserFirstDetails/Drive");
                drivetag.InnerText = drive;

                FileInfo fp = new FileInfo(org1);
                fp.Attributes = FileAttributes.Normal;
                UsrDetails.Save(org1);

                DirectoryInfo diSource = new DirectoryInfo(sourcedrive+"Palle_University");
                DirectoryInfo diTarget = new DirectoryInfo(tbDrive.Text+":\\Palle_University");

                CopyAll(diSource, diTarget);
                this.Hide();
            }
            else 
            {
                errorproviderDrive.SetError(tbDrive, "Length should be one character only");
            }

        }
        else
        {
            errorproviderDrive.SetError(tbDrive, "Drive Should not be empty");
        }
    }


 public  void CopyAll(DirectoryInfo source, DirectoryInfo target)
    {

        DirectoryInfo di = Directory.CreateDirectory(target.FullName);
        // Copy each file into the new directory.
        foreach (FileInfo fi in source.GetFiles())
        {
            Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
            fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
        }

        // Copy each subdirectory using recursion.
        foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
        {
            DirectoryInfo nextTargetSubDir =
                target.CreateSubdirectory(diSourceSubDir.Name);
            CopyAll(diSourceSubDir, nextTargetSubDir);
        }
    }

1 个答案:

答案 0 :(得分:1)

要计算要在进度条控件中显示的文件复制操作的进度,您需要获取源目录中的文件总数

首先声明两个通用变量

int totalFilescount; //Total number of files in the source directory
int currentFileindex; //Incremented when a file is copied to the destination directory

在调用CopyAll之前,您需要获取源目录中的文件总数

    totalFilescount = diSource.GetFiles("*", SearchOption.AllDirectories).Length;

在CopyAll方法中,对于每个复制的文件,您可以增加currentFileindex,然后可以使用以下方法计算进度

     double progressVal = (double)(currentFileindex * 100 )/ totalFilescount;

修改CopyAll方法以报告进度

public void CopyAll(DirectoryInfo source, DirectoryInfo target)
{

    DirectoryInfo di = Directory.CreateDirectory(target.FullName);
    // Copy each file into the new directory.

    foreach (FileInfo fi in source.GetFiles())
    {
        Console.WriteLine(@"Copying {0}\{1}", target.FullName, fi.Name);
        fi.CopyTo(Path.Combine(target.FullName, fi.Name), true);
        currentFileindex += 1;

     double progressVal = (double)(currentFileindex * 100 )/ totalFilescount;

    if (progressVal <= 100)
    {
        progressBar1.Value =Convert.ToInt32(Math.Floor(progressVal));
    }

    }

    // Copy each subdirectory using recursion.
    foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
    {
        DirectoryInfo nextTargetSubDir =
            target.CreateSubdirectory(diSourceSubDir.Name);
        CopyAll(diSourceSubDir, nextTargetSubDir);
    }
}