[英]Recursive delete of files and directories in C#
How to delete a given directory recursively in C#?如何在 C# 中递归删除给定目录? A directory containing files.
包含文件的目录。
Should the System.IO.Directory.Delete with the second parameter true
do 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.我首先提出这个问题的原因是,恰好调用
Delete
(第二个参数设置为true
)的代码没有做它应该做的事情。 As it turned out the cause of that was that there was a file somewhere down in the the directory hierarchy with RO
attribute set, and the Polish version of Windows XP
was throwing a really strange message for that.事实证明,其原因是在目录层次结构中的某个位置设置了
RO
属性,而波兰语版本的Windows XP
正在为此抛出一个非常奇怪的消息。
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") );
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.)
(我刚刚仔细检查过,它对我来说很好用。)
If you get UnauthorizedAccessException .如果您收到 UnauthorizedAccessException 。 You can use modified of RecursiveDelete from Jone Polvora .
您可以使用Jone Polvora中RecursiveDelete 的修改。 Thank you for Idea.
谢谢你的想法。 I will use it.
我会用的。
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();
}
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
Modified solution from @StayOnTarget, so that root dir is not getting removed:来自@StayOnTarget 的修改解决方案,因此不会删除根目录:
public static void RecursiveDelete(DirectoryInfo baseDir, bool isRootDir)
{
if (!baseDir.Exists)
return;
foreach (var dir in baseDir.EnumerateDirectories()) RecursiveDelete(dir, false);
foreach (var file in baseDir.GetFiles())
{
file.IsReadOnly = false;
file.Delete();
}
if (!isRootDir) baseDir.Delete();
}
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.