C#删除文件夹以及该文件夹中的所有文件和文件夹 [英] C# delete a folder and all files and folders within that folder

查看:2657
本文介绍了C#删除文件夹以及该文件夹中的所有文件和文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试删除文件夹以及该文件夹中的所有文件和文件夹,我正在使用下面的代码,我收到错误文件夹不为空关于我可以做什么的任何建议?

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆