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#?

- 49,173
- 15
- 109
- 139

- 13,012
- 23
- 97
- 162
-
Windows Explorer, cmd, some other file manager, a shell open/save dialog. There are more things than just Explorer that can hold this open. – Joey Jan 22 '10 at 15:54
-
Do you want to identify whether Windows Explorer, specifically, has it open, or just whether some other process has an open handle to it? And what do you want to accomplish? Catching the exception is a pretty good indication that some other process has a handle to it, if that's all you're aiming for. – Dathan Jan 22 '10 at 15:57
-
@Dathan Catching exception is good idea, but we have very large code base multi threaded application. It's good if we identify weather we have accees to it. Thanks – Prashant Cholachagudda Jan 22 '10 at 16:02
-
1but what good is knowing you have access to it, if there is no guarantee that you will still have access to it when you have finished the check? – Sam Holder Jan 22 '10 at 16:05
-
@bebop I agree but I want have some defensive code against automated test :) – Prashant Cholachagudda Jan 22 '10 at 16:08
-
but what if its not open in just explorer? what if its open in command prompt? Or some save as dialog in an application? are you going to check for all those things? or you could just catch the exception and deal with it. – Sam Holder Jan 22 '10 at 16:15
-
@bebop I completly agree with you but is there any why to find out whether a folder/file being used by another process? NOTE: Not by handling exception – Prashant Cholachagudda Jan 22 '10 at 16:18
3 Answers
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?

- 32,535
- 13
- 101
- 181
-
Yeah, this is how I'd go about it. You should probably be monitoring this anyway. – Toji Jan 22 '10 at 15:56
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;
}

- 733,204
- 149
- 1,241
- 1,454
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.

- 1
- 1

- 32,535
- 13
- 101
- 181