删除异步文件&进度条中的子文件夹和显示状态百分比

时间:2016-01-08 16:27:26

标签: c# file timer count percentage

基本上我有一个包含一些文件和其他文件夹(也有文件)的文件夹,我想通过在两个进度条窗口窗体控件中显示操作的百分比来执行主删除。 第一个进度条应显示“正在删除z目录中y文件的x文件”,第二个进度条显示整个进程百分比。

第一个进度条示例:删除(1/100)D:\ folder \ file.extension (其中1是要删除的文件的编号,100是从该文件夹中计算的文件数)但是如果有人可以告诉我一种方法只使用1个进度条(第1个),那将非常有用。

到目前为止,这是我的代码:

 private int deleted = 0;
 private int total = 0;

    private void RemoveDirectories(string strpath)
    {
        if (Directory.Exists(strpath))
        {
            DirectoryInfo dirInfo = new DirectoryInfo(strpath);

            foreach (FileInfo file in dirInfo.GetFiles())
            {
                //file.Delete();
                total += (int) file.Length;
                deleted /= 1024;
                progressBar1.Step = deleted;
                progressBar1.PerformStep();

            }
            foreach (DirectoryInfo dir in dirInfo.GetDirectories())
            {
                //dir.Delete(true);
                total += dir.GetFiles().Length;
                deleted /= 1024;
                progressBar1.Step = deleted;
            }
        }
    }

    private void button1_Click(object sender, EventArgs e)
    {
        RemoveDirectories(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\dex");
    }

1 个答案:

答案 0 :(得分:1)

有一些错误:

1-删除总是0,所以它不会做任何事情    2步意味着栏增加多少,应该是1    3-您没有设置Max值,因此它总是100    4 - 你在UI线程上做了所有这些,它将阻止UI,直到操作完成,所以即使你纠正了以前的错误,你只会看到从0到最大的跳转,你需要在一个工人中执行此操作线程并使用Invoke()

更新UI

以下是您的功能修改:

    private void RemoveDirectories(string strpath)
    {
        ThreadPool.QueueUserWorkItem((o) =>
        {
            if (Directory.Exists(strpath))
            {
                DirectoryInfo dirInfo = new DirectoryInfo(strpath);
                var files = dirInfo.GetFiles();
                 //I assume your code is inside a Form, else you need a control to do this invocation;
                this.BeginInvoke(new Action(() =>
                {
                    progressBar1.Minimum = 0;
                    progressBar1.Value = 0;
                    progressBar1.Maximum = files.Length;
                    progressBar1.Step = 1;
                }));

                foreach (FileInfo file in files)
                {
                    //file.Delete();
                    this.BeginInvoke(new Action(() => progressBar1.PerformStep())); //I assume your code is inside a Form, else you need a control to do this invocation;

                }

                var dirs = dirInfo.GetDirectories();

                this.BeginInvoke(new Action(() =>
                {
                    progressBar1.Value = 0;
                    progressBar1.Maximum = dirs.Length;
                }));

                foreach (DirectoryInfo dir in dirs)
                {
                    //dir.Delete(true);
                    this.BeginInvoke(new Action(() => progressBar1.PerformStep())); //I assume your code is inside a Form, else you need a control to do this invocation;
                }

            }
        }, null);
    }