473,385 Members | 1,342 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,385 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 21 '05 #1
1 7260
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 21 '05 #2

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

Similar topics

3
by: violin wang | last post by:
hi I wrote a windows based program.The main UI named form_base has a button "btn1".In btn1`s click event handler I used ShowDialog(this) to show another form named form_test.form_test also has a...
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: 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: 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: 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....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.