c# - 为什么我会收到异常目录不为空?

标签 c# winforms

<分区>

我有这一行:

Directory.Delete(outputfiles, true);

如果我将它设置为 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: 

最佳答案

根据 MSDN :

If the recursive parameter is true, the user must have write permission for the current directory as well as for all subdirectories.

可能是用户没有写权限?

或者这个:

In some cases, if you have the specified directory open in File Explorer, the Delete method may not be able to delete it.

关于c# - 为什么我会收到异常目录不为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18204088/

相关文章:

c# - 无法授权 Azure LogAnalytics 工作区

winforms - c# TypeConverter 和 InstanceDescriptor 的痛苦

winforms - 如何通过执行基本操作(非交互式)来测试应用程序是否经常工作

c# - 每次按下按钮时,如何从字符串的前面连续移动n个字符到末尾?

c# - 将数据从子项聚合到父项以进行绑定(bind)

c# - 如何捕捉新进程的启动和停止?

c# - 具有数据库连接的 IIS Web 应用程序

c# - .NET 值类型在内存中的布局

c# - 如何检测任务栏上的右键单击

c# - 跨线程操作无效