"目录不为空"试图以编程方式删除文件夹时出错 [英] "Directory is not empty" error when trying to programmatically delete a folder

查看:316
本文介绍了"目录不为空"试图以编程方式删除文件夹时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,我建立了一个系统,用户可以创建图片的画廊。照片在磁盘CATEGORY_NAME / GALLERY_NAME / {图片}的格式在文件夹中保持。上面给出每个上传的照片存储相关的目录结构。

在试图删除虽然一个类别,以及从数据库中删除,我想从系统中删除相关文件夹了。当我第一次收到错误信息目录不是空的我的searched发现此解决方案:

 公共静态无效DeleteDirectory(字符串TARGET_DIR)
    {
        字符串[] =文件Directory.GetFiles(TARGET_DIR);
        字符串[] =迪尔斯Directory.GetDirectories(TARGET_DIR);        的foreach(在文件中字符串的文件)
        {
            File.SetAttributes(文件,FileAttributes.Normal);
            File.Delete(文件);
        }        的foreach(在迪尔斯串DIR)
        {
            DeleteDirectory(DIR);
        }        Directory.Delete(TARGET_DIR,FALSE);
    }

通过该解决方案,在GALLERY_NAME文件夹中的照片被删除就好了,那么GALLERY_NAME文件夹本身被删除很好..所以我们现在只剩下一个空文件夹CATEGORY_NAME。然后,code在上述子程序的最后一位( Directory.Delete(TARGET_DIR,FALSE); )被调用来删除文件夹CATEGORY_NAME。该错误再次引起..

有谁知道解决这个?


  1. Directory.Delete(TARGET_DIR,真); 没有工作,这就是为什么我试图替代

  2. 我有超过父文件夹和CATEGORY_NAME和GALLERY_NAME文件夹也编程方式创建没有任何问题的完全控制。

  3. 正如我所提到的,子目录(GALLERY_NAME文件夹)及其内容(照片),与此code就好了删除。这是导致错误的文件夹CATEGORY_NAME,尽管这code之后,它只是一个空文件夹。

异常消息我得到的是:

  System.IO.IOException是由用户code未处理
  的HResult = -2147024751
  消息=目录不是空的。  来源= mscorlib程序
  堆栈跟踪:
       在System.IO .__ Error.WinIOError(的Int32错误code,字符串maybeFullPath)
       在System.IO.Directory.DeleteHelper(FULLPATH字符串,字符串userPath,布尔递归,布尔throwOnTopLevelDirectoryNotFound)
       在System.IO.Directory.Delete(FULLPATH字符串,字符串userPath,布尔递归,布尔checkHost)
       在System.IO.Directory.Delete(字符串路径)
       在D中MyApp.PhotoGallery.Categories.deleteCategory(CID的Int32,字符串类别名):\\ Documents \\我的Dropbox \\ web项目\\ MyAppFolder \\ APP_ code \\ BLL \\ PhotoGallery.vb:291线
       在_admhades_PhotoGallery.deleteCategory(的Int32的categoryID,字符串类别名)在d:\\ Documents \\我的Dropbox \\ web项目\\ Havadis pre \\ _admhades \\ PhotoGallery.aspx.vb:行71


解决方案

您可以只使用 Directory.Delete(TARGET_DIR,真实); 来删除目录和递归的所有文件。你不必编写自定义的功能。

In my app, I have built a system where users can create picture galleries. Photos held in folders in the format of category_name/gallery_name/{pictures} on disk. Each uploaded photo is stored under relevant directory structure as given above.

When trying to delete a category though, as well as deleting from the database, I want to delete relevant folders from the system too. When I first received the error message "Directory is not empty" I searched and found this solution:

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);
    }

With this solution, photos in the "gallery_name" folder gets deleted just fine, then the gallery_name folder itself gets deleted fine.. so we are now left with an empty category_name folder. Then the last bit of code in the above subroutine (Directory.Delete(target_dir, false);) gets called to delete the category_name folder. The error raises again..

Does anyone knows a solution to this?

  1. Directory.Delete(target_dir, true); did not work, that is why I tried an alternative.
  2. I have full control over the parent folder and the category_name and gallery_name folders are also created programmatically without any problem.
  3. As I mentioned, the sub directories (gallery_name folders) and their contents (photos) are deleted with this code just fine. It is the category_name folder which causes the error, even though after this code, it is just an empty folder.

The exception message I get is:

System.IO.IOException was unhandled by user code
  HResult=-2147024751
  Message=The directory is not empty.

  Source=mscorlib
  StackTrace:
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive, Boolean throwOnTopLevelDirectoryNotFound)
       at System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive, Boolean checkHost)
       at System.IO.Directory.Delete(String path)
       at MyApp.PhotoGallery.Categories.deleteCategory(Int32 cID, String categoryname) in d:\Documents\My Dropbox\web projects\MyAppFolder\App_Code\BLL\PhotoGallery.vb:line 291
       at _admhades_PhotoGallery.deleteCategory(Int32 categoryID, String categoryname) in d:\Documents\My Dropbox\web projects\HavadisPre\_admhades\PhotoGallery.aspx.vb:line 71

解决方案

You may just use Directory.Delete(target_dir, true); to remove directory and all files recursively. You don't have to write custom function.

这篇关于"目录不为空"试图以编程方式删除文件夹时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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