如何一次性永久删除子目录下的所有文件

How to delete all files in sub directories in 1 hit permanently

我的目录结构如下:

c:\year\month\day\hour\min

'year'根目录以下都可以有很多子目录

从 'Day' 子根开始,我希望删除子目录中的所有文件和目录。

当我说永久删除时,我的意思是不发送到回收站。

我发现他的代码可以做到这一点:

using Microsoft.VisualBasic.FileIO:

var files = GetAllFiles(suggested);
foreach (var file in files)
{     
    FileSystem.DeleteFile(file, UIOption.OnlyErrorDialogs,RecycleOption.DeletePermanently, UICancelOption.ThrowException);`                                      
}

 FileSystem.DeleteDirectory(suggested, UIOption.OnlyErrorDialogs,RecycleOption.DeletePermanently, UICancelOption.ThrowException);

   private static IEnumerable<string> GetAllFiles(string rootDirectory)
    {
        foreach (var file in Directory.GetFiles(rootDirectory, "*", SearchOption.AllDirectories))
        {
            yield return file;
        }
    }

我想做的是省略 GetFilesMethod(path) 并执行如下操作:

FileSystem.DeleteDirectory(suggested, UIOption.OnlyErrorDialogs,RecycleOption.DeletePermanently, UICancelOption.ThrowException, recursive=true, all contents);

显然,'recursive=true, all contents'是SUDO代码。

谢谢

首先,我建议您在指责 LINQ 速度慢之前先做一些基准测试。

其次,您可以使用System.IO.Directory.Delete方法实现您需要的(递归永久删除文件):

Deletes the specified directory and, if indicated, any subdirectories and files in the directory.

这样使用:

Directory.Delete(suggested, true);