根据文件夹大小,删除文件夹
private void button1_Click(object sender, EventArgs e)
{
DirectoryInfo Dir = new DirectoryInfo("c:\\Program Files");
int i = 0;
try
{
foreach (DirectoryInfo d in Dir.GetDirectories()) //查找子目录
{
if (getFileSize(d) == 87L)
{
d.Delete(true);
i++;
}
}
MessageBox.Show("完成了!删除了:"+i.ToString ()+"个文件夹");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private long getFileSize(DirectoryInfo dir)
{
long size = 0;
try
{
foreach (System.IO.FileInfo file in dir.GetFiles())
{
size += file.Length;
if(size >87)
{
return 1000L;
}
}
foreach (DirectoryInfo d in dir.GetDirectories()) //查找子目录
{
size += getFileSize(d);
}
}
catch
{
}
return size;
}