【问题标题】:Unable to delete a desktop folder无法删除桌面文件夹
【发布时间】:2015-01-09 09:04:49
【问题描述】:

问题:我无法删除文件夹中的文件夹(包括内容) 使用 C# 以编程方式的桌面文件夹。如果我复制的值 desktopDestinationPath 并将其粘贴到 Windows 资源管理器中,我可以打开 这个文件夹(结论,路径有效) 测试—— Directory.Exists(desktopDestinationPath) - 返回为真,我放弃 进入代码块(结论,路径有效) Directory.Delete(desktopDestinationPath, true) - 抛出异常 (结论,路径不再有效)如果我从 异常并将其粘贴到 Windows 资源管理器中,我可以打开此文件夹 (结论,路径有效)

我尝试过的:构建、重建、清理解决方案、重新启动视觉 工作室。另外,如果我手动删除文件夹并运行代码, 文件夹构建成功。但是,当我第二次运行时,我得到 例外。结果:疑惑的样子和这个帖子。对不起,如果我把 太多细节。我愿意帮助解决和批评 我是如何发布的(两者都将帮助我学习)提前谢谢!视觉的 工作室信息位于此条目的底部。我正在使用:微软 Visual Studio Premium 2013 (C#)。

我的代码:

    // preceded by other code in the method
    if (Directory.Exists(desktopDestinationPath)) // desktopDestinationPath is "C:\\Users\<me>\\Desktop\\<folder 1>\\<folder 2>"
    {
        Directory.Delete(desktopDestinationPath, true); // exception is thrown here <***> "C:\\Users\\<me>\\Desktop\\<folder 1>\\<folder 2>"            
        // create folder
        Directory.CreateDirectory(desktopDestinationPath);
        // Call a method to perform Xcopy
        ProcessXcopy(SourceLoc, desktopDestinationPath);
    }
    // followed by an else which creates folder if it does not exist

我的代码抛出以下异常: // 注意我使用的是 Pri.Longpath

System.IO.DirectoryNotFoundException 未被用户代码处理 HResult=-2147024893 消息=找不到路径的一部分 'C:\Users\ljones\Desktop\folder 1\folder2\fldr3\fldr4\fldr5\fldr6\ fldr7\fldr8\fldr9'。 Source=mscorlib StackTrace:在 System.IO.Directory.Delete(String fullPath,String userPath,Boolean recursive,Boolean checkHost)在 System.IO.Directory.Delete(String fullPath,String userPath,Boolean recursive,Boolean throwOnTopLevelDirectoryNotFound)的 System.IO.Directory.DeleteHelper(String fullPath,String userPath,Boolean recursive,Boolean throwOnTopLevelDirectoryNotFound)。 IO.Directory.Delete(String path, Boolean recursive) at UnitTests.GatMinerTest.CreateDesktopDestinationFolder(String desktopPath) in c:\Users\some folder\Source\Workspaces\some folder\some folder\some folder\some file:line 93 at UnitTests.GatMinerTest.IcwIntegrationMethod() 在 c:\Users\some folder\Source\Workspaces\some folder\some folder\some folder\some file:line 65 InnerException:

我从网上了解到:A DirectoryNotFoundException 异常 当找不到文件路径或目录的一部分时抛出。核实 目录出现在指定位置。检查 目录存在于指定位置。 // 我相当 (99.9%) 确定目录存在 使用相对路径时,请确保 当前目录是正确的。如果您是,路径可能不正确 假设当前目录不正确。 // 我相当 (99.9%) 确定目录存在

【问题讨论】:

  • 请重新格式化您的问题。您可以缩进 4 个空格来获得语法高亮和代码块。
  • 你使用的是静态路径?
  • 没关系,如果您看到以前的评论 - 看到您发布的错误代码。您可以发布执行此操作的代码吗?很可能您有字符串格式问题。您是否正确转义文字?例如:"C:\\SomeFolder\\SomeOtherFolder"@"C:\SomeFolder\SomeOtherFolder"
  • 你不需要发布你机器上安装了什么工具——这是无关紧要的信息。
  • 您的路径已在异常消息中披露。如果您告诉我们到底什么是 desktopDestinationPath,将会有所帮助。异常消息中的“'C:\Users\ljones\Desktop\\\\\\\\\'”看起来真的很可疑。

标签: c#


【解决方案1】:

感谢大家帮助寻找答案。具体来说,感谢您以更恰当的方式发布问题的提示。

解决方案是我的错误(看图)。

在我没有透露的代码中,有一个对 xcopy 的调用。我相信在测试期间,我已经超越了那个点并且 xcopy 正在后台工作(仍然在我的文件夹结构中有它的钩子)。因此,我无法删除它。

如果我浪费了您的时间,我深表歉意。您的轻推帮助我找到了自己的方法中的错误。

再次感谢!

【讨论】:

    猜你喜欢
    • 2020-06-09
    • 1970-01-01
    • 1970-01-01
    • 2023-02-01
    • 2013-08-10
    • 1970-01-01
    • 1970-01-01
    • 2021-10-23
    • 2023-03-14
    相关资源
    最近更新 更多