473,398 Members | 2,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

How to make the RestoreDirectory property work in an OpenFileDialog

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
Nov 16 '05 #1
1 2308
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

Johann Blake wrote:
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

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: bobrad | last post by:
MAYBE SOMEBODY CAM HELP I AM USING THE FOLLOWING CODE TO GET A LOCATION OF A FILE IN A WINFORM DIALOG OpenFileDialog openFileDialog = new OpenFileDialog(); OpenFileDialog.Filter = " Access...
2
by: don | last post by:
has anyone experienced problems with interactions between OpenFileDialog and FileStream? When I select "open" from an OpenFileDialog the subsequent calls to FileStream do not create a file. If I do...
8
by: e-mid | last post by:
why does not openFileDialog have closed event? i want to do something; as soon as the dialog closes. is there a way to do this?
1
by: Austin Jan | last post by:
Hi all, In this code snip try { using(OpenFileDialog dlg = new OpenFileDialog()) { //do something }
1
by: Olaf Baeyens | last post by:
In .NET v1.1 I am trying to select multiple files using OpenFileDialog like this OpenFileDialog openFileDialog=new OpenFileDialog(); openFileDialog.Multiselect=true; i...
0
by: wavemill | last post by:
Hello! I would like add an openfiledialog in the property of my usercontrol than we add image property. Have got any idea, or tutorial? Thank you, Wavemill
1
by: Johann Blake | last post by:
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...
1
by: sam | last post by:
OpenFileDialog appears to be "holding onto" the parent folder of a selected file until the application using the dialog quits. After creating a folder containing a file, I attempted to select...
3
by: Martijn Mulder | last post by:
It strikes me that System.Windows.Forms.OpenFileDialog seems te 'remember' which directory it was in last, even when a new OpenFileDialog-object is created for every access to the file system....
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.