In FileOpen or FileSave dialogbox when I call them, they automatically go to the last opened path. This happens even if I close my application and open it. But how to get that path/file name to a textbox or variable?
5 Answers
it seems a bit weired but under Windows 7 it works with the folling:
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);
Try it and tell me if you need further help.

- 2,435
- 1
- 24
- 30
-
I am trying to get the value not set it. I need to get the last opened file name. – Vicky Apr 19 '11 at 11:39
Presumably the information is stored somewhere in the depths of the registry (it's done by the unmanaged control to which OpenFileDialog
is only a wrapper). The easiest would probably be to persist the path the last time the dialog was closed in your application somewhere where you can access it.

- 18,612
- 4
- 58
- 83
-
But I have a stange behaviour, in the application developed in VB .net the value in textbox is getting retained even when application is closed, but not in that deve;oped in C# – Vicky Apr 19 '11 at 11:41
-
The `OpenFileDialog` does not rememeber the last file name - only the last directory. – ChrisWue Apr 19 '11 at 21:24
As mentioned here: What does the FileDialog.RestoreDirectory Property actually do?
They use the path from Environment.CurrentDirectory
.
I am having a similar problem to Vicky, which goes as follows. I am developing in Visual Basic 2008 Express Edition under Vista Business SP2.
I have an application with an OpenFileDialog and a SaveFileDialog. When I call the OpenFileDialog on first running the application, it defaults to the directory from which the Application last opened/saved a file. However, this directory IS NOT "Environment.CurrentDirectory" which is set to "C:\Users\Brian\Documents\Visual Studio 2008\Projects\IFPM Analysis\IFPM Analysis\bin\Debug" and is not changed by either the OpenFileDialog or SaveFileDialog.
Later on in the Application, I call the SaveFileDialog, with the initial directory property (.InitialDirectory) set in the code to a default directory. When I subsequently call the OpenFileDialog, it defaults to the directory used by the SaveFileDialog. All the time, the value of "Environment.CurrentDirectory" is unchanged.
So, my question is, where is the directory that is being used by the OpenFileDialog and SaveFileDialog being stored? I assume it is something to do with the underlying FileDialog class, and I know persists even after the Application has been closed and restarted.
Ideally I want to be able to store the directory selected by the user from the OpenFileDialog and reset it after I have used the SaveFileDialog. While I can use the InitialDirectory property of the OpenFileDialog within the Application, this doesn't help me when I close the Application and restart it. Sadly, the typical user actions are:
- start Application
- open file with OpenFileDialog
- save file with SaveFileDialog
- leave Application
This means that when the user comes back to the Application, the default directory is the "wrong" one. I know that I can save the last used directory from the OpenFileDialog in my own configuration file so that it will persist outside of the Application, but that seems a little silly when Windows provides me with the same functionality, if only I knew where to look!
Any help gratefully received!
Thanks, Brian.

- 19
- 1
-
This is exactly what I want :) I taught the same regarding the idea of saving details to a config file... – Vicky Apr 22 '11 at 12:06
-
1I now have a solution. I created a property in the My.Settings Object called CurrentDirectory. I then explicitly set it to the directory selected by the user each time the File Dialog was called (I had to extract this from the file name selected). I also used the File Dialog's .InitialDirectory method to set the directory to My.Settings.CurrentDirectory. This persists after the Application has been closed and thus solves my problem. Brian. – Brian Williams Apr 22 '11 at 22:59
-
If you have a question you should create another thread, not ask it in the answers. – actaram Aug 11 '17 at 12:44
The recent opened files list is stored in 2 places:
- Recent Folder: The recent folder is usually located under C:\Documents and Settings[Your Profile]\Recent (The path is different under Windows Vista), and it contains shortcuts to the recently opened files.
- Registry: Each time that a file is selected in save/open dialog-box, the filename is added to the files list under
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSaveMRU
This method can help you to get the list:
public static string GetLastOpenSaveFile(string extention)
{
RegistryKey regKey = Registry.CurrentUser;
string lastUsedFolder = string.Empty;
regKey = regKey.OpenSubKey("Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\ComDlg32\\OpenSaveMRU");
if (string.IsNullOrEmpty(extention))
extention = "html";
RegistryKey myKey = regKey.OpenSubKey(extention);
if (myKey == null && regKey.GetSubKeyNames().Length > 0)
myKey = regKey.OpenSubKey(regKey.GetSubKeyNames()[regKey.GetSubKeyNames().Length - 2]);
if (myKey != null)
{
string[] names = myKey.GetValueNames();
if (names != null && names.Length > 0)
{
lastUsedFolder = (string)myKey.GetValue(names[names.Length - 2]);
}
}
return lastUsedFolder;
}
Success! Iordan

- 11
- 2