none
How to get the last directory after open FileDialog

    Question

  • Set FileDialog.RestoreDirectory Property true. When reopenning the file dialog box, it locates the last directory.

    My question is "How can I get the last directory?"

    Thanks,
    -Andrew
    Tuesday, September 02, 2008 4:29 PM

Answers

  • RestoreDirectory property makes sure that the value in Environment.CurrentDirectory will be reset before the OpenFileDialog closes.  If RestoreDirectory is set to false, then Environment.CurrentDirectory will be set to whatever directory the OpenFileDialog was last open to.  So, if Environment.CurrentDirectory is "C:\" before you run an OpenFileDialog, and then you run an OpenFileDialog where the RestoreDirectory value is set to true, then after the user selects a file, the Environment.CurrentDirectory will still be "C:\".  If the RestoreDirectory property is set to false, then Environment.CurrentDirectory will reflect whatever folder contains the file the user selected. 

    Do something like this:

    // save your current directory  
    string currentDirectory = Environment.CurrentDirectory;  
     
    // create an OpenFileDialog and set RestoreCurrentDirectory to false.   
    OpenFileDialog ofd = new OpenFileDialog();  
    ofd.RestoreCurrentDirectory = false;  
    ofd.ShowDialog();  
     
    // save the selected directory locally.   
    string selectedDirectory = Environment.CurrentDirectory;  // OpenFileDialog changed this value.   
    Environment.CurrentDirectory = currentDirectory; // reset the property with the first value.   
     
    // next time you open an OpenFileDialog, set the InitialDirectory property  
    OpenFileDialog ofd2 = new OpenFileDialog();  
    ofd.InitialDirectory = selectedDirectory; // set the InitialDirectory to what it was last time an OpenFileDialog was opened.   
    ofd.ShowDialog(); 




    David Morton - http://blog.davemorton.net/
    Tuesday, September 02, 2008 4:49 PM
  • It stores it in the registry.  HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Comdlg32\OpenSaveMRU.
    Hans Passant.
    Wednesday, September 03, 2008 3:34 AM

All replies

  • RestoreDirectory property makes sure that the value in Environment.CurrentDirectory will be reset before the OpenFileDialog closes.  If RestoreDirectory is set to false, then Environment.CurrentDirectory will be set to whatever directory the OpenFileDialog was last open to.  So, if Environment.CurrentDirectory is "C:\" before you run an OpenFileDialog, and then you run an OpenFileDialog where the RestoreDirectory value is set to true, then after the user selects a file, the Environment.CurrentDirectory will still be "C:\".  If the RestoreDirectory property is set to false, then Environment.CurrentDirectory will reflect whatever folder contains the file the user selected. 

    Do something like this:

    // save your current directory  
    string currentDirectory = Environment.CurrentDirectory;  
     
    // create an OpenFileDialog and set RestoreCurrentDirectory to false.   
    OpenFileDialog ofd = new OpenFileDialog();  
    ofd.RestoreCurrentDirectory = false;  
    ofd.ShowDialog();  
     
    // save the selected directory locally.   
    string selectedDirectory = Environment.CurrentDirectory;  // OpenFileDialog changed this value.   
    Environment.CurrentDirectory = currentDirectory; // reset the property with the first value.   
     
    // next time you open an OpenFileDialog, set the InitialDirectory property  
    OpenFileDialog ofd2 = new OpenFileDialog();  
    ofd.InitialDirectory = selectedDirectory; // set the InitialDirectory to what it was last time an OpenFileDialog was opened.   
    ofd.ShowDialog(); 




    David Morton - http://blog.davemorton.net/
    Tuesday, September 02, 2008 4:49 PM
  • It stores it in the registry.  HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Comdlg32\OpenSaveMRU.
    Hans Passant.
    Wednesday, September 03, 2008 3:34 AM
  • David M Morton said:

    RestoreDirectory property makes sure that the value in Environment.CurrentDirectory will be reset before the OpenFileDialog closes.  If RestoreDirectory is set to false, then Environment.CurrentDirectory will be set to whatever directory the OpenFileDialog was last open to.  So, if Environment.CurrentDirectory is "C:\" before you run an OpenFileDialog, and then you run an OpenFileDialog where the RestoreDirectory value is set to true, then after the user selects a file, the Environment.CurrentDirectory will still be "C:\".  If the RestoreDirectory property is set to false, then Environment.CurrentDirectory will reflect whatever folder contains the file the user selected. 

    Do something like this:

    // save your current directory  
    string currentDirectory = Environment.CurrentDirectory;  
     
    // create an OpenFileDialog and set RestoreCurrentDirectory to false.   
    OpenFileDialog ofd = new OpenFileDialog();  
    ofd.RestoreCurrentDirectory = false;  
    ofd.ShowDialog();  
     
    // save the selected directory locally.   
    string selectedDirectory = Environment.CurrentDirectory;  // OpenFileDialog changed this value.   
    Environment.CurrentDirectory = currentDirectory; // reset the property with the first value.   
     
    // next time you open an OpenFileDialog, set the InitialDirectory property  
    OpenFileDialog ofd2 = new OpenFileDialog();  
    ofd.InitialDirectory = selectedDirectory; // set the InitialDirectory to what it was last time an OpenFileDialog was opened.   
    ofd.ShowDialog(); 




    David Morton - http://blog.davemorton.net/



    Thanks, David.

    My question may not be clear. For my scenario:
    1. Run a form application with pressing a button to open a file dialog box,  the directory may be changed into "C:\temp", for example, remember/save the path before closing the openfiledialog. Setting RestoreCurrentDirectory property to True can do it.
    2. The application closes.
    3. The application is run again. When oppening the openfiledialog box, the path "C:\temp" as its InitialDirectory.

    My question is: How to get the path "C:\temp"?

    Thanks,
    -Andrew

    Wednesday, September 03, 2008 2:29 PM
  • Asked and answered.
    Hans Passant.
    Wednesday, September 03, 2008 11:04 PM
  • It stores it in the registry.  HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Comdlg32\OpenSaveMRU.
    Hans Passant.

    Where is it stored in Vista? That key does not exist in Vista.
    -Andy.
    Sunday, September 20, 2009 7:38 PM
  • Looking at Win7, the Comdlg32 key is still there but the data is completely reorganized.  Binary now, I see little hope of reverse-engineering that.

    Hans Passant.
    Sunday, September 20, 2009 8:04 PM
  • Looking at Win7, the Comdlg32 key is still there but the data is completely reorganized.  Binary now, I see little hope of reverse-engineering that.

    Hans Passant.

    So what would be the "MS approved" method of retrieving the "last opened" directory of a OpenFileDialog in .NET??
    -Andy.
    Sunday, September 20, 2009 8:09 PM
  • There is none.  You don't need one, just don't set the InitialDirectory property and it is automatic.

    Hans Passant.
    Sunday, September 20, 2009 8:20 PM
  • There is none.  You don't need one

    Hans Passant.
    Actually, I do.

    I want to select the most recent file when the user uses an openfiledialog. How do I determine what folder the openfiledialog is about to open to? I could save the folder path in a settings file, but that will only work after the 1st time the dialog is opened. Besides, why should I have to do this if it's already being stored somewhere? Quite frustrating.

    -Andy.
    Sunday, September 20, 2009 8:31 PM
  • Use your imagination.

    Keep the value of the last path used and use it as your initial directory.
    John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com
    • Proposed as answer by CarlosMartini Sunday, February 20, 2011 12:53 AM
    • Unproposed as answer by CarlosMartini Sunday, February 20, 2011 12:53 AM
    Sunday, September 20, 2009 8:34 PM

    • 1st you check if your current directory contains your root.
                if (!Environment.CurrentDirectory.Contains(System.Configuration.ConfigurationSettings.AppSettings["MacroDirectory"].ToString()))
                {
                    mySaveFileDialog.InitialDirectory = System.Configuration.ConfigurationSettings.AppSettings["MacroDirectory"].ToString();
                }
                else
                {
                    mySaveFileDialog.InitialDirectory = Environment.CurrentDirectory;
                }
    • At the end of your event, make sure you save the Environment.CurrentDirectory
    • Environment.CurrentDirectory = mySaveFileDialog.FileName.Substring(0, mySaveFileDialog.FileName.LastIndexOf("\\"));
    • That will save your Environment.CurrentDirectory for subsequent openings
    • Proposed as answer by CarlosMartini Sunday, February 20, 2011 1:00 AM
    Sunday, February 20, 2011 12:58 AM