How to identify whether folder is opened?

岁酱吖の 提交于 2019-12-01 17:45:32

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?

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;
}
Sam Holder

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!