为什么我得到异常目录不为空?

Dan*_*est 1 c# winforms

我有这条线:

Directory.Delete(outputfiles, true);
Run Code Online (Sandbox Code Playgroud)

如果我将其设置为true那么它也应该删除子目录和文件.现在我检查了该目录是否为空,因此下次将删除该目录.但在此目录中的异常之前,我有另外4个子目录,其中一个我有一个大约7 Mb大小的zip文件.

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

  Source=mscorlib
  StackTrace:
       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 Diagnostic_Tool_Blue_Screen.CreateDirectories.CreateDirectoriesAtConstructor() in d:\C-Sharp\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\CreateDirectories.cs:line 35
       at Diagnostic_Tool_Blue_Screen.Form1..ctor() in d:\C-Sharp\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Form1.cs:line 125
       at Diagnostic_Tool_Blue_Screen.Program.Main() in d:\C-Sharp\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Program.cs:line 19
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: 
Run Code Online (Sandbox Code Playgroud)

Fio*_*ite 8

根据MSDN:

如果recursive参数为true,则用户必须具有当前目录以及所有子目录的写权限.

可能是用户没有写入权限?

或者这一个:

在某些情况下,如果在文件资源管理器中打开了指定的目录,则Delete方法可能无法将其删除.

  • 我在本地开发时遇到同样的错误,事实证明,如果您在资源管理器中打开文件夹,则会抛出异常。 (2认同)