tags:

views:

152

answers:

5

Which directory does FileDialog (OpenFileDialog/SaveFileDialog) uses?
When I show it from my application it opens folder which is not Environment.CurrentDirectory folder. How can I get this directory path? Or it's controlled by the OS and I can't know it?

A: 

you can set it with "InitialDirectory" property of OpenFileDialog.

MNZ
The question isn't "how to set it", but "how to get it"... the InitialDirectory property is empty until it is set by the calling code
Thomas Levesque
+2  A: 

According to MSDN:

On Windows Vista, if InitialDirectory is set to a full file name instead of just a directory path, the initial directory will default either to the application path, or to the directory from which the user last selected a file.

Otherwise it doesn't seem to say anything, for me it looked like the Environment.SpecialFolder.MyDocuments, I'm using Vista.

Yuriy Faktorovich
+1  A: 

This information is stored in the registry on a per-application basis, so you should be able to retrieve it. I don't remember the exact registry location, I'll try to find it...

EDIT:

On Windows7, it is stored in HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedPidlMRU

The value names are just numbers, and the content is encoded in some binary format, so it's probably not very easy to read...

Thomas Levesque
A: 

You could make a extern call to GetOpenFileName function to see what the initial OPENFILENAME structure stores (looking at the lpstrInitialDir).

From MSDN: lpstrInitialDir
Pointer to a NULL terminated string that can specify the initial directory.

You could also check the following registry key: HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\LastVisitedMRU\

There is a string value named MRUList with an order of indexes named to the binary value. If you open the binary value of the first index (whichever letter it is) in the MRUList you will see the name of your application and the directory.

EDIT: You might find the code at the bottom of this discussion useful if you are trying to set/get it manually.

SwDevMan81
If I make a call to GetOpenFileName, the dialog box will appear. I just need to know what directory is a "current directory" for my application.
nightcoder
A: 

How about looking at the source code to the method that shows it, and see what string it uses?

silverCORE
don't have VS installed here or I would've done it for you.
silverCORE
It just calls GetOpenFileName winapi function. And it seems like that function internally decides which folder will be initial one if it gets null in OPENFILENAME.lpstrInitialDir structure member.
nightcoder