How to make the RestoreDirectory property work in an OpenFileDialog

J

Johann Blake

A number of posts have indicated developers having problem getting an
OpenFileDialog to startup in a specific directory when the dialog is
shown. The first time you show the dialog, the InitialDirectory will
cause the dialog to show files in that directory. However, any further
times that the dialog is displayed, the directory that is shown will
always be the one initially used regardless of the RestoreDirectory
setting. This appears to be a behaviour on Windows 2000/XP.

To solve the problem, simply create a *new* OpenFileDialog object at
runtime each time you need to display the dialog. Do this rather than
referencing an existing object that is on your form. The object on the
form gets only instantiated once and preserves its values while at the
same time ignoring the RestoreDirectory property. So here is an
example. Place a OpenFileDialog control on your form as you usually
do. In this example, call it dlgFileSelect. When you go to display it,
here is an example of what code you would use to:

this.dlgSelectFile = new OpenFileDialog();
this.dlgSelectFile.InitialDirectory = @"c:\somedirectory";
this.dlgSelectFile.RestoreDirectory = true;
this.dlgSelectFile.ShowDialog(this);

Johann Blake
 
M

Mike Newton

Geesh.

That particular dialog is always pokey for me when it's first loaded. I
suspect that it has to do with the fact that it checks out my CD and
network drives. Couldn't we just reset the initialDirectory property,
and show the same object?

Creating a new one means that I have to sit there waiting for the dialog
to be created every time.

Mike
 

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