简体   繁体   English

C#删除一个文件夹以及该文件夹内的所有文件和文件夹

[英]C# delete a folder and all files and folders within that folder

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?我正在尝试删除一个文件夹以及该文件夹中的所有文件和文件夹,我正在使用下面的代码,但出现错误Folder is not empty ,对我能做什么有什么建议吗?

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

Read the Manual:阅读手册:

Directory.Delete Method (String, Boolean) Directory.Delete 方法(字符串,布尔值)

Directory.Delete(folderPath, true);

Try:尝试:

System.IO.Directory.Delete(path,true)

This will recursively delete all files and folders underneath "path" assuming you have the permissions to do so.假设您有权限这样做,这将递归删除“路径”下的所有文件和文件夹。

Err, what about just calling Directory.Delete(path, true);呃,只是调用Directory.Delete(path, true);怎么样Directory.Delete(path, true); ? ?

Directory.Delete方法有一个递归布尔参数,它应该做你需要的

You should use:你应该使用:

dir.Delete(true);

for recursively deleting the contents of that folder too.也用于递归删除该文件夹的内容。 See MSDN DirectoryInfo.Delete() overloads .请参阅 MSDN DirectoryInfo.Delete() 重载

Try this.尝试这个。

namespace EraseJunkFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo yourRootDir = new DirectoryInfo(@"C:\somedirectory\");
            foreach (DirectoryInfo dir in yourRootDir.GetDirectories())
                    DeleteDirectory(dir.FullName, true);
        }
        public static void DeleteDirectory(string directoryName, bool checkDirectiryExist)
        {
            if (Directory.Exists(directoryName))
                Directory.Delete(directoryName, true);
            else if (checkDirectiryExist)
                throw new SystemException("Directory you want to delete is not exist");
        }
    }
}

对于那些遇到 DirectoryNotFoundException 的人,请添加以下检查:

if (Directory.Exists(path)) Directory.Delete(path, true);
public void Empty(System.IO.DirectoryInfo directory)
{
    try
    {
        logger.DebugFormat("Empty directory {0}", directory.FullName);
        foreach (System.IO.FileInfo file in directory.GetFiles()) file.Delete();
        foreach (System.IO.DirectoryInfo subDirectory in directory.GetDirectories()) subDirectory.Delete(true);
    }
    catch (Exception ex)
    {
        ex.Data.Add("directory", Convert.ToString(directory.FullName, CultureInfo.InvariantCulture));

        throw new Exception(string.Format(CultureInfo.InvariantCulture,"Method:{0}", ex.TargetSite), ex);
    }
}

Try This:尝试这个:

foreach (string files in Directory.GetFiles(SourcePath))
{
   FileInfo fileInfo = new FileInfo(files);
   fileInfo.Delete(); //delete the files first. 
}
Directory.Delete(SourcePath);// delete the directory as it is empty now.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM