OpenFileDialog (Constant Inital Directory)


N

NvrBst

I drag the OpenFileDialog into the WZYWIG editor and set all its
properties. Setting its' InitalDirectory does make the first time it
opens, open up in the right directory. However, everytime after that
it remembers the directory that the user navigated too and opens
initally at that directory there after.

*Begining of My Open Button Event*
//Environment.CurrentDirectory = Application.StartupPath;
//openFileDialog1.InitialDirectory = Application.StartupPath;
//openFileDialog1.RestoreDirectory = true;
//openFileDialog1.RestoreDirectory = false;
openFileDialog1.ShowDialog();


I've tried many permutations of the above. The openFileDialog1
initally shows "C:\Documents\...\bin\Release\" directory, but then I
open a file in "C:\" and then hit the "Open" button again and it
starts at "C:\".

Could somone explain what exactly RestoreDirectory is suppose to do
since I've tried with it set to true and false and it doesn't seem to
change the behavour that I can see? When it says "Restores CWD before
closing" does it mean when its Disposed?

Solutions. The only way I can seem to have the openFileDialog always
open up in a specific directory is to a) always create a new
openfiledialog everytime I want to use one. or b) call the
"openFileDialog1.Reset()" function and re-set all my filters/
initaldirectory etc.

Am I missing something? I'm using VS2008, .NET Framework 3.5.

NB
 
Ad

Advertisements

N

NvrBst

[...]
Could somone explain what exactly RestoreDirectory is suppose to do
since I've tried with it set to true and false and it doesn't seem to
change the behavour that I can see?  When it says "Restores CWD before
closing" does it mean when its Disposed?

That property controls what the "current working directory" state is after 
the dialog is closed.  By default, the common file dialogs change the  
working directory as the user navigates within the file system.  If you  
set that property to "true", the working directory will still be changed,  
but the dialog will restore it to the original value when the dialog is  
closed.

Unfortunately for you, the working directory doesn't actually have  
anything to do with what the initial directory is for the dialog.
Solutions.  The only way I can seem to have the openFileDialog always
open up in a specific directory is to a) always create a new
openfiledialog everytime I want to use one. or b) call the
"openFileDialog1.Reset()" function and re-set all my filters/
initaldirectory etc.

Well, you could just always set the InitialDirectory property before using 
the dialog.  However, I'm curious as to what would be so bad about option  
"a"?  I never drag components like that into my Form, even though it's  
supported in the Designer.  I don't see any reason for them to live any  
longer than the specific place in which I need them.

By the way, this seems like a decent place to use an extension method.  
You can create one for the OpenFileDialog class that shows the dialog with 
your defaults.  Then, wherever you want to show the dialog, you create a 
new one normally, and then call your extension method instead of  
ShowDialog().  The extension method would then set whatever defaults you 
want (including the InitialDirectory property), and then itself call  
ShowDialog(), returning the result from that method to the caller.

Pete


Ahh I see what Restore Directory is doing. It indeed is reseting the
CWD when set to true/false, but yes, doesn't seem to help me.

Setting(or reseting) the "InitalDirectory" before calling the
ShowDialog() only works the inital time, it seems to remember its last
directory (in an unknown variable someplace? - I thought it was using
the CWD initally, but it isn't) and after it sets that value it
doesn't matter what the InitalDirectory is. I've even tried
"InitialDirectory = null; InitialDirectory = Application.StartUpPath;"
with no success.

Reasion I used a shared OpenFileDialog is because I have about 3
buttons that open up simular types of files (in different windows), so
instead of setting my filters/titles/default ext, etc each time I only
did it in one place. This way also VS takes care of the dispose on it
so I don't have to do: using(OpenFileDialog xxx = new
OpenFiledialog()) { ... } each time :) But thats not really an issue
for me :)

The extension method will work for me though :) I just have it call
Reset(), then set all my values, then it'll call ShowDialog(this).
This way, at the very least, the settings for the dialog will be
localized so I can change them easier if needed.

I'm a little curious how the OpenFileDialog remembers its directory
though, just a private (unaccessable) variable, or some kind of global
path that might be changable (Like the CWD?)?

NB
 
N

NvrBst

[...]
I'm not sure I understand the question.  Any class can retain any data it  
wants so long as the instance of that class still exists.  Since you're  
always using the same instance of the OpenFileDialog, I don't find it  
mysterious at all that it could remember its directory.  Where it actually  
remembers it is an implementation detail and should be of no concern to  
anyone using the class.  I have no reason to believe that the retained  
data is global in nature, especially since creating a new instance of the  
dialog resets it.  :)

I thought maybe if I knew where it was looking to get the directory to
show then I could change it without having to Reset() :) Or complain
on why it isn't public! hehe

I didn't realize it was just hiding the dialog after I pressed the
Okay/Cancle buttons, which is probably what the reset/dispose methods
clean up. I think I'm understanding it better :) Thanks for all the
info :)

NB
 
Ad

Advertisements

N

NvrBst

Ahh, after playing around with it for a bit in debug mode I found out
how to reset the location without reseting the entire object. You
simply have to reset the FileName before using it. It'll then default
to the InitalDirectory (which should already be set): IE


openFileDialog1.FileName = "*your file name, or blank*";
openFileDialog1.ShowDialog(this);


If the FileName is set to something, IE "C:\tst.exe" then it'll open
at "C:\" regardless of what InitalDirectory is set at.

Cheers, NB
 

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