如何判断文件夹是否打开?

发布于 2024-08-18 15:19:35 字数 117 浏览 2 评论 0原文

在我的应用程序中,我尝试重命名该文件夹,但如果在 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 技术交流群。

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

发布评论

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

评论(3

厌倦 2024-08-25 15:19:35

捕获 IOException?

正如其他人所说,只要尝试做你想做的事情,捕获发生的异常并采取适当的操作,无论你的上下文是什么。

在我看来,您实际上没有太多选择,请考虑:

bool iHaveAccess = CheckAccess(folder);
if (iHaveAccess)
{
    RenameFolder(folder,newFolderName);
}

如果在 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:

bool iHaveAccess = CheckAccess(folder);
if (iHaveAccess)
{
    RenameFolder(folder,newFolderName);
}

what happens if between CheckAccess succeeding and calling RenameFolder something else locks the folder? Whatcha gonna do then?

子栖 2024-08-25 15:19:35

确定程序是否以阻止您重命名文件夹的方式打开文件夹是不合理的。因为在您做出决定后,另一个进程可能会立即启动或停止使用该文件夹。相反,只需执行操作并捕获生成的异常即可。

try {
  Directory.Move("old","new");
  return true;
} catch ( IOException ) {
  return false;
}

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.

try {
  Directory.Move("old","new");
  return true;
} catch ( IOException ) {
  return false;
}
锦爱 2024-08-25 15:19:35

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.

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