不允许我删除刚刚移出文件的文件夹吗?

发布于 2024-10-01 21:19:48 字数 380 浏览 0 评论 0原文

这是我的问题:我刚刚完成了一个应用程序,它允许您右键单击资源管理器中的文件并点击打开该应用程序的选项。然后,它会重命名该文件并将其从文件夹移出到特定位置,然后删除该文件夹(无论是否为空)。当通过 GUI 加载文件时,这很好用,但资源管理器上下文菜单的全部要点是 GUI 永远不需要加载,它只是单个文件的一击奇迹。

但是,当使用上下文菜单时,该文件夹不会删除,并且会引发异常,表明该文件夹正在被另一个进程使用。该文件仍会被移动,所有其他文件仍会被删除。为什么会发生这种情况?有什么方法可以显式释放目录上的句柄以使其工作吗?

我会向您展示代码,但它非常基本(即一行,Dir​​ectory.Delete)。我也尝试过使用 DirectoryInfo.Delete 但这没有帮助。就像我说的,它在 GUI 中工作得很好。

Here's my problem: I just finished up an app which allows you to right-click on a file in Explorer and hit an option to open the app. It then renames and moves that file out of the folder to a specific location, and then deletes the folder (empty or not). This works fine when the files are loaded through the GUI, but the whole point of the explorer context menu is that the GUI never has to load, it's just a one-hit-wonder for single files.

However, when using the context menu, the folder doesn't delete and it throws an exception saying the folder is in use by another process. The file still gets moved and all other files still get deleted. Why is this happening? Is there some way I can explicitly release the handle on the directory in order to make this work?

I'd show you the code but it's extremely basic (ie one line, Directory.Delete). I've also tried using DirectoryInfo.Delete but that didn't help. Like I say, it works fine from within the GUI.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨落星ぅ辰 2024-10-08 21:19:48

我是否正确地假设从 GUI 中删除意味着该文件夹当前未在资源管理器中打开,而当您使用上下文菜单删除时该文件夹(显然)是打开的?如果是这样,此博客可能有一些洞察问题。另外,这是另一个问题 询问如何关闭资源管理器窗口。可能有帮助。

“Windows NT 系列操作系统保持打开进程当前目录的句柄。”

Am I correct in assuming that deleting from the GUI means that the folder is not currently open in explorer whereas when you delete using the context menu the folder is (obviously) open? If so, this blog might have some insight into the problem. Also, here's another SO question which asked how to close an explorer window. Might help.

"Windows NT family of operating systems keeps open a handle to the process's current directory."

淡莣 2024-10-08 21:19:48

我认为发生的情况是资源管理器在复制或移动文件时尝试删除该文件夹。您需要做的是创建一个单独的线程,首先移动或复制文件(或多个文件),然后当该线程完成时,删除该文件夹。

I think whats happening is explorer is trying to delete the folder while the file is being copied or moved. What you need to do is create a seperate thread that moves or copies the file (or files) first, then when that thread completes, delete the folder.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文