C#删除一个文件夹以及该文件夹内的所有文件和文件夹 [英] C# delete a folder and all files and folders within that folder
本文介绍了C#删除一个文件夹以及该文件夹内的所有文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试删除一个文件夹以及该文件夹中的所有文件和文件夹,我正在使用下面的代码,但出现错误 Folder is not empty
,关于我可以做什么的任何建议做什么?
I'm trying to delete a folder and all files and folders within that folder, I'm using the code below and I get the error Folder is not empty
, any suggestions on what I can do?
try
{
var dir = new DirectoryInfo(@FolderPath);
dir.Attributes = dir.Attributes & ~FileAttributes.ReadOnly;
dir.Delete();
dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[i].Index);
}
catch (IOException ex)
{
MessageBox.Show(ex.Message);
}
推荐答案
dir.Delete(true); // true => recursive delete
这篇关于C#删除一个文件夹以及该文件夹内的所有文件和文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文