无法使用 Directory.Delete(path, true) 删除目录 [英] Cannot delete directory with Directory.Delete(path, true)

查看:46
本文介绍了无法使用 Directory.Delete(path, true) 删除目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .NET 3.5,尝试使用以下方法递归删除目录:

I'm using .NET 3.5, trying to recursively delete a directory using:

Directory.Delete(myPath, true);

我的理解是,如果文件正在使用或存在权限问题,这应该抛出,否则它应该删除目录及其所有内容.

My understanding is that this should throw if files are in use or there is a permissions problem, but otherwise it should delete the directory and all of its contents.

但是,我偶尔会得到这个:

However, I occasionally get this:

System.IO.IOException: The directory is not empty.
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)
    at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)
    ...

我对方法有时会抛出并不感到惊讶,但是当递归为真时,我很惊讶收到此特定消息.(我知道目录不是空的.)

I'm not surprised that the method sometimes throws, but I'm surprised to get this particular message when recursive is true. (I know the directory is not empty.)

有什么理由让我看到这个而不是 AccessViolationException?

Is there a reason I'd see this instead of AccessViolationException?

推荐答案

编者注:虽然这个答案包含了一些有用的信息,但实际上关于 Directory.Delete<的工作原理是错误的/代码>.请阅读此答案的评论以及此问题的其他答案.

Editor's note: Although this answer contains some useful information, it is factually incorrect about the workings of Directory.Delete. Please read the comments for this answer, and other answers to this question.

我之前遇到过这个问题.

I ran into this problem before.

问题的根源在于该功能不会删除目录结构内的文件.因此,您需要做的是创建一个函数,在删除目录本身之前先删除目录结构中的所有文件,然后删除所有目录.我知道这与第二个参数背道而驰,但这是一种更安全的方法.此外,您可能希望在删除文件之前立即从文件中删除 READ-ONLY 访问属性.否则会引发异常.

The root of the problem is that this function does not delete files that are within the directory structure. So what you'll need to do is create a function that deletes all the files within the directory structure then all the directories before removing the directory itself. I know this goes against the second parameter but it's a much safer approach. In addition, you will probably want to remove READ-ONLY access attributes from the files right before you delete them. Otherwise that will raise an exception.

只需将此代码添加到您的项目中即可.

Just slap this code into your project.

public static void DeleteDirectory(string target_dir)
{
    string[] files = Directory.GetFiles(target_dir);
    string[] dirs = Directory.GetDirectories(target_dir);

    foreach (string file in files)
    {
        File.SetAttributes(file, FileAttributes.Normal);
        File.Delete(file);
    }

    foreach (string dir in dirs)
    {
        DeleteDirectory(dir);
    }

    Directory.Delete(target_dir, false);
}

另外,对我个人而言,我个人对允许删除的机器区域进行了限制,因为您是否希望有人在 C:WINDOWS (%WinDir%) 上调用此函数或C:.

Also, for me I personally add a restriction on areas of the machine that are allowed to be deleted because do you want someone to call this function on C:WINDOWS (%WinDir%) or C:.

这篇关于无法使用 Directory.Delete(path, true) 删除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆