删除异步文件和子文件夹并在进度条中显示状态百分比
Delete Async Files & Subfolder and Show Status Percentage in Progressbar
基本上我有一个文件夹,里面有一些文件和其他文件夹(也有文件),我想通过在两个进度条 windows 表单控件中显示操作的百分比来执行主删除.
第一个进度条应显示 "x file from y files from z directory is being deleted",第二个进度条应显示整体进程百分比。
第一个进度条示例:删除 (1/100) D:\folder\file.extension
(其中 1 是要删除的文件数,100 是该文件夹中的文件数)但是如果有人能告诉我一种方法也只有一个进度条(第一个),那将非常有帮助。
到目前为止,这是我的代码:
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- deleted 始终为 0,因此不会执行任何操作
2-Step 表示 bar 会增加多少,应该是 1
3- 你没有设置最大值,所以它总是 100
4-你在 UI 线程上做了所有这些,它会阻塞 UI 直到操作完成,所以即使你纠正了之前的错误,你也只会看到从 0 到 max 的跳转,您需要在工作线程中执行此操作并使用 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);
}
基本上我有一个文件夹,里面有一些文件和其他文件夹(也有文件),我想通过在两个进度条 windows 表单控件中显示操作的百分比来执行主删除. 第一个进度条应显示 "x file from y files from z directory is being deleted",第二个进度条应显示整体进程百分比。
第一个进度条示例:删除 (1/100) D:\folder\file.extension (其中 1 是要删除的文件数,100 是该文件夹中的文件数)但是如果有人能告诉我一种方法也只有一个进度条(第一个),那将非常有帮助。
到目前为止,这是我的代码:
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- deleted 始终为 0,因此不会执行任何操作 2-Step 表示 bar 会增加多少,应该是 1 3- 你没有设置最大值,所以它总是 100 4-你在 UI 线程上做了所有这些,它会阻塞 UI 直到操作完成,所以即使你纠正了之前的错误,你也只会看到从 0 到 max 的跳转,您需要在工作线程中执行此操作并使用 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);
}