如何判断文件夹是否打开?
在我的应用程序中,我尝试重命名该文件夹,但如果在 Windows 资源管理器中打开该文件夹,我会收到 IOException
。如何在 C# 中判断文件夹是否在 Windows 资源管理器中打开?
In my application I'm trying to rename the folder, but if the folder is opened in Windows Explorer I get an IOException
. How can I identify whether folder is opened in Windows Explorer in C#?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
捕获 IOException?
正如其他人所说,只要尝试做你想做的事情,捕获发生的异常并采取适当的操作,无论你的上下文是什么。
在我看来,您实际上没有太多选择,请考虑:
如果在 CheckAccess 成功和调用 RenameFolder 之间有其他东西锁定文件夹,会发生什么?那要做什么?
catch the IOException?
As others have said, just try to do what you want, catch the exception if it happens and take appropriate action, whatever that is in your context.
You don't really have much choice as I see it, consider:
what happens if between CheckAccess succeeding and calling RenameFolder something else locks the folder? Whatcha gonna do then?
确定程序是否以阻止您重命名文件夹的方式打开文件夹是不合理的。因为在您做出决定后,另一个进程可能会立即启动或停止使用该文件夹。相反,只需执行操作并捕获生成的异常即可。
It is not reasonable to determine if a program has a folder open in such a way that prevents you from renaming it. Because immediately after you make the determination, another process could start or stop using the folder. Instead just do the operation and catch the resulting exception.
经过一番搜索后,我发现 这篇文章 和这篇文章< /a> 显示了如何以编程方式确定哪个进程锁定了文件的各种技术。其中之一应该允许您检查资源管理器是否已锁定该文件夹。
after a little searching I found this post and this post which show various techniques for how you can programatically determine which process has locked a file. One of those should allow you to check if explorer has the folder locked.