清空目录的最佳方法是什么?

时间:2022-11-16 18:08:01

Is there a way to delete all files & sub-directories of a specified directory without iterating over them?

有没有办法删除指定目录的所有文件和子目录而不迭代它们?

The non elegant solution:

非优雅的解决方案:

public static void EmptyDirectory(string path)
{
    if (Directory.Exists(path))
    {
        // Delete all files
        foreach (var file in Directory.GetFiles(path))
        {
            File.Delete(file);
        }

        // Delete all folders
        foreach (var directory in Directory.GetDirectories(path))
        {
            Directory.Delete(directory, true);
        }
    }
}

3 个解决方案

#1


How about System.IO.Directory.Delete? It has a recursion option, you're even using it. Reviewing your code it looks like you're trying to do something slightly different -- empty the directory without deleting it, right? Well, you could delete it and re-create it :)

System.IO.Directory.Delete怎么样?它有一个递归选项,你甚至使用它。检查你的代码看起来你正试图做一些稍微不同的事情 - 清空目录而不删除它,对吧?好吧,你可以删除它并重新创建它:)


In any case, you (or some method you use) must iterate over all of the files and subdirectories. However, you can iterate over both files and directories at the same time, using GetFileSystemInfos:

在任何情况下,您(或您使用的某些方法)必须遍历所有文件和子目录。但是,您可以使用GetFileSystemInfos同时迭代文件和目录:

foreach(System.IO.FileSystemInfo fsi in 
    new System.IO.DirectoryInfo(path).GetFileSystemInfos())
{
    if (fsi is System.IO.DirectoryInfo)
        ((System.IO.DirectoryInfo)fsi).Delete(true);
    else
        fsi.Delete();
}

#2


Why is that not elegant? It's clean, very readable and does the job.

为什么不优雅?它干净,非常易读,能完成任务。

#3


Well, you could always just use Directory.Delete....

好吧,你总是可以使用Directory.Delete ....

http://msdn.microsoft.com/en-us/library/aa328748%28VS.71%29.aspx

Or if you want to get fancy, use WMI to delete the directory.

或者,如果您想获得想象力,请使用WMI删除目录。

#1


How about System.IO.Directory.Delete? It has a recursion option, you're even using it. Reviewing your code it looks like you're trying to do something slightly different -- empty the directory without deleting it, right? Well, you could delete it and re-create it :)

System.IO.Directory.Delete怎么样?它有一个递归选项,你甚至使用它。检查你的代码看起来你正试图做一些稍微不同的事情 - 清空目录而不删除它,对吧?好吧,你可以删除它并重新创建它:)


In any case, you (or some method you use) must iterate over all of the files and subdirectories. However, you can iterate over both files and directories at the same time, using GetFileSystemInfos:

在任何情况下,您(或您使用的某些方法)必须遍历所有文件和子目录。但是,您可以使用GetFileSystemInfos同时迭代文件和目录:

foreach(System.IO.FileSystemInfo fsi in 
    new System.IO.DirectoryInfo(path).GetFileSystemInfos())
{
    if (fsi is System.IO.DirectoryInfo)
        ((System.IO.DirectoryInfo)fsi).Delete(true);
    else
        fsi.Delete();
}

#2


Why is that not elegant? It's clean, very readable and does the job.

为什么不优雅?它干净,非常易读,能完成任务。

#3


Well, you could always just use Directory.Delete....

好吧,你总是可以使用Directory.Delete ....

http://msdn.microsoft.com/en-us/library/aa328748%28VS.71%29.aspx

Or if you want to get fancy, use WMI to delete the directory.

或者,如果您想获得想象力,请使用WMI删除目录。