Retrieving the real initial directory before a file dialog is opened?

K

Keith

Hello,

I'm trying to get the "about to be used" directory from an Open/Save file
dialog. I'm not talking about InitialDirectory (which only seems to be
useful if you are setting it) but the directory whose contents will be
displayed when the dialog is shown if InitialDirectory is untouched.

For example, if the user previously browsed to directory FOO using the
SaveFileDialog, I'd like to be able to get that directory from the next
instance of the dialog, BEFORE it is shown or any user interaction takes
place. It is obviously persisted, since it opens there automatically if
InitialDialog is not set, but I can't seem to find the right property or
method to request it. Do I have to get if from somewhere in the Registry?
Is it a system property? An environment setting?

// Nonsesical example code
private void SomeFunction()
{
SaveFileDialog sfd1 = new SaveFileDialog();
sfd1.ShowDialog(); // assume the user browsed to directory A and
saved.

SaveFileDialog sfd2 = new SaveFileDialog();
// Here I want to know what the directory used by sfd1 was.
InitialDirectory is "", but sfd2
// will open to directory A regardless. Where is this value stored?
How can I get it?
sfd2.ShowDialog();
}

Thanks for reading!

Keith
 
O

Oliver Sturm

Keith said:
An environment setting?

Exactly. The dialogs set the application's working directory, which you
can access (and set yourself) at any time via
Environment.CurrentDirectory. As long as you have only one
Open-/SaveFileDialog, that one will track its current directory itself,
but if you want to find out the most recently used directory to
synchronize multiple dialog components, you need to use the Environment
property.



Oliver Sturm
 
K

Keith

Oliver,

Thanks for the reply. I'm not tracking multiple dialogs, but rather doing a
look-ahead to generate appropriate filenames (Document1, Document2, etc.) to
using in the FileDialog.

Regardless, your tip will work great for me. I suppose if I want to persist
this directory between instances of my app I'll need to write it out to the
Registry or something.

Thanks again!

Keith
 
K

Keith

Regardless, your tip will work great for me. I suppose if I want to
persist
this directory between instances of my app I'll need to write it out to
the Registry or something.

Ok,

Maybe this won't work so well for me. Environment.CurrentDirectory is only
updated after the dialog is shown once; then it contains the last dir used
by the dialog. Before that, it contains the dir from which the process was
started. However, when the dialog is shown, it opens to the correct dir
(the last one used, even between instances of the app) regardless.

So where does the dialog persist this information? It can't be using
CurrentDirectory, since it doesn't contain the correct information; it must
be stored somewhere else. I'd like to be able to get the info right from
the source.

Any ideas?

Keith
 
O

Oliver Sturm

Keith said:
So where does the dialog persist this information? It can't be using
CurrentDirectory, since it doesn't contain the correct information; it must
be stored somewhere else. I'd like to be able to get the info right from
the source.

There's a registry key at
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32,
which is used for this purpose. Maybe it's documented somewhere, but
I've never tried to read and parse this information. It's categorized by
file type, so it might not be that useful to your own application -
maybe it would be better if you just stored away your own last path and
set it again on restart.



Oliver Sturm
 
K

Keith

Oliver,

Thanks for all the suggestions!

K
Oliver Sturm said:
There's a registry key at
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32,
which is used for this purpose. Maybe it's documented somewhere, but I've
never tried to read and parse this information. It's categorized by file
type, so it might not be that useful to your own application - maybe it
would be better if you just stored away your own last path and set it
again on restart.



Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
 

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