OpenFileDialog Handle Leak

N

NickP

Hi there,

This is really crazy!

1. Make a folder
2. Put a text file in the folder
3. Run the following code...

Dim pop As New OpenFileDialog
Using pop
Call pop.ShowDialog()
End Using

4. Browse for the newly created text file
5. Press OK on the OpenFileDialog
6. Now try to delete the folder you created in step 1.

On my system I am being informed that a process is using the folder,
which it is, but why? The OpenFileDialog and SaveFileDialog automatically
instantiate a file handle to the folder when pressing OK? They dont make a
handle to the text file, only the folder...

Any ideas on how I can destroy this handle, I presumed disposing the
OpenFileDialog object would be adequate but obviously not as it should be
disposed by the 4th line.

Nick.
 
N

NickP

BTW, the handle is released once the application is closed, so don't close
it until you have tried step 6.
 
J

Jared Parsons [MSFT]

Hello NickP,
BTW, the handle is released once the application is closed, so don't
close it until you have tried step 6.

The problem here is the RestoreDirectory property. By default this is set
to false. By default calling ShowDialog() on OpenFileDialog will start in
the current directory for the process. If you change directories in the
dialog it will also change the current working directory of the process.
Unless you set RestoreDirectory process to True the last directory you enter
in the dialog will be the working directory of the process when you finish.
That's why you still have an open handle to the directory.
 
N

NickP

Hi Jared,

Thanks for the info, so presumably I should be able to elliminate this
issue by changing the current working directory before attempting to delete
said folder? I'll have a try of that, many thanks and much appreciation :)

Nick.

Jared Parsons said:
Hello NickP,
BTW, the handle is released once the application is closed, so don't
close it until you have tried step 6.

The problem here is the RestoreDirectory property. By default this is set
to false. By default calling ShowDialog() on OpenFileDialog will start in
the current directory for the process. If you change directories in the
dialog it will also change the current working directory of the process.
Unless you set RestoreDirectory process to True the last directory you
enter in the dialog will be the working directory of the process when you
finish. That's why you still have an open handle to the directory.
--
Jared Parsons [MSFT]
(e-mail address removed)
All opinions are my own. All content is provided "AS IS" with no
warranties, and confers no rights.
 
J

Jared Parsons [MSFT]

Hello NickP,
Hi Jared,

Thanks for the info, so presumably I should be able to elliminate
this issue by changing the current working directory before attempting
to delete said folder? I'll have a try of that, many thanks and much
appreciation :)

Another quick fix is to set RestoreDirectory to true. That will undo the
directory change as soon as the dialog closes
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top