Tom Goff's .Net Musings

Tidbits of information regarding .Net, C#, and SQL Server.

Who’s Locking My Folder?

with one comment

Recently, I ran across a problem deleting a folder used by my application because the folder “was being used by another process”. Using Process Explorer, I was able to determine that it was my application that was holding the lock on the directory.

Because of the steps to reproduce, I was able to narrow down the method that was locking the folder fairly quickly. Once I found the problem method, I set a break point at the start of the method and fired up my application. When the break point was hit, I stepped over the first statement then checked Process Explorer to see if the folder in question was locked. I repeated these steps for each statement in the method, until Process Explorer reported the folder as open by my application.

The offending statement was a call to show an open file dialog (via the OpenFileDialog class). I proceeded by tweaking/removing the various properties of the OpenFileDialog being set by my code. When I changed the RestoreDirectory property from false to true, then the folder was no longer being locked.

MSDN says that if the RestoreDirectory property is true, then the “dialog box restores the current directory to its original value if the user changed the directory while searching for files”. If RestoreDirectory is false, then it will not restore the current directory to its original value and will maintain the directory previously selected by the user. It is apparent that the file dialog(s) open a handle to the directory previously selected by the user in order to use it as the initial directory the next time a file dialog is shown.

Using Lutz Roeder’s .Net Reflector, I was able to determine that the directory handle is opened by the Windows API (and is not specific to the .Net runtime). Specifically, the OFN_NOCHANGEDIR flag of the OPENFILENAME structure corresponds to the RestoreDirectory property.

To be clear, I don’t consider this a bug. It is just one of those gotchas that can have you tearing your hair out if you don’t know about it. A sample project can be downloaded from here.

Advertisements

Written by Tom

April 2, 2007 at 1:25 pm

Posted in .Net, C#, Gotcha

One Response

Subscribe to comments with RSS.

  1. Tom,

    just wanted to let you know I was one of those guys tearing his hair out because of the OpenDialog bug (sorry, “intended behaviour”) which you describe. It took me a while to get into the right direction of possible causes and let google point me to your usefull article. Thanks.

    Ossan Dust

    May 24, 2007 at 6:44 am


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: