C#递归删除文件和目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/925192/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 03:20:54  来源:igfitidea点击:

Recursive delete of files and directories in C#

c#

提问by Bartosz Radaczyński

How to delete a given directory recursively in C# ? A directory containing files.

如何在 C# 中递归删除给定目录?包含文件的目录。

Should the System.IO.Directory.Deletewith the second parameter truedo the trick?

带有第二个参数的System.IO.Directory.Delete是否应该起作用true



EDIT:

编辑:

So, I actually did answer my own question, although the answers here were a little more clarifying. The reason for me asking this in the first place was that the code that has exactly that invocation of Delete(2nd param set to true) was not doing what it was supposed to be doing. As it turned out the cause of that was that there was a file somewhere down in the the directory hierarchy with ROattribute set, and the Polishversion of Windows XPwas throwing a really strange message for that.

所以,我实际上确实回答了我自己的问题,尽管这里的答案更明确一些。我首先提出这个问题的原因是,完全调用Delete(2nd param set to true)的代码没有做它应该做的事情。事实证明,其原因是在目录层次结构中某处的某个文件RO设置了属性,波兰语版本Windows XP为此抛出了一个非常奇怪的消息。

采纳答案by Jon Skeet

Yup, that's the point of that parameter. Did you try it and have any problems? (I've just double-checked, and it works fine for me.)

是的,这就是该参数的重点。您是否尝试过并遇到任何问题?(我刚刚仔细检查过,它对我来说很好用。)

回答by Marc Gravell

Recursive works for both files and folders (oddly, I thought it didn't work for files; my bad...):

递归适用于文件和文件夹(奇怪的是,我认为它不适用于文件;我的坏...):

// create some nested folders...
Directory.CreateDirectory(@"c:\foo");
Directory.CreateDirectory(@"c:\foo\bar");
// ...with files...
File.WriteAllText(@"c:\foo\blap.txt", "blup");
File.WriteAllText(@"c:\foo\bar\blip.txt", "blop");
// ...and delete them
Directory.Delete(@"c:\foo", true); // fine

回答by Jone Polvora

The only solution that worked for me if the subdirectories also contains files is by using a recursive function:

如果子目录还包含文件,唯一对我有用的解决方案是使用递归函数:

    public static void RecursiveDelete(DirectoryInfo baseDir)
    {
        if (!baseDir.Exists)
            return;

        foreach (var dir in baseDir.EnumerateDirectories())
        {
            RecursiveDelete(dir);
        }
        baseDir.Delete(true);
    }

It appears that Directory.Delete(dir, true) only delete files of the current directory, and subdirectories if they are empty.

看起来 Directory.Delete(dir, true) 只删除当前目录的文件,如果它们是空的子目录。

Hope it helps someone.

希望它可以帮助某人。

btw, example: RecursiveDelete( new DirectoryInfo(@"C:\my_dir") );

顺便说一句,例如: RecursiveDelete( new DirectoryInfo(@"C:\my_dir") );

回答by karpediemnow

Why do not use?

为什么不使用?

Directory.Delete(directoryPath, true);

Directory.Delete(directoryPath, true);

https://msdn.microsoft.com/en-us/library/fxeahc5f(v=vs.110).aspx

https://msdn.microsoft.com/en-us/library/fxeahc5f(v=vs.110).aspx

回答by Константин Золин

If you get UnauthorizedAccessException . You can use modified of RecursiveDeletefrom Jone Polvora. Thank you for Idea. I will use it.

如果您收到 UnauthorizedAccessException 。您可以使用修改的RecursiveDelete卓尼Polvora。谢谢你的想法。我会用的。

    public static void RecursiveDelete(DirectoryInfo baseDir)
    {
        if (!baseDir.Exists)
            return;

        foreach (var dir in baseDir.EnumerateDirectories())
        {
            RecursiveDelete(dir);
        }
        var files = baseDir.GetFiles();
        foreach (var file in files)
        {
            file.IsReadOnly = false;
            file.Delete();
        }
        baseDir.Delete();
    }