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