OpenFileDialog Handle Leak

  • Thread starter Thread starter NickP
  • Start date Start date
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.
 
BTW, the handle is released once the application is closed, so don't close
it until you have tried step 6.
 
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.
 
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.
 
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
 
Back
Top