locked
How to get the last directory after open FileDialog RRS feed

  • Pergunta

  • 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
    terça-feira, 2 de setembro de 2008 16:29

Respostas

  • 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/
    terça-feira, 2 de setembro de 2008 16:49
  • It stores it in the registry.  HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Comdlg32\OpenSaveMRU.
    Hans Passant.
    quarta-feira, 3 de setembro de 2008 03:34

Todas as Respostas

  • 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/
    terça-feira, 2 de setembro de 2008 16:49
  • It stores it in the registry.  HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Comdlg32\OpenSaveMRU.
    Hans Passant.
    quarta-feira, 3 de setembro de 2008 03:34
  • 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

    quarta-feira, 3 de setembro de 2008 14:29
  • Asked and answered.
    Hans Passant.
    quarta-feira, 3 de setembro de 2008 23:04
  • 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.
    domingo, 20 de setembro de 2009 19:38
  • 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.
    domingo, 20 de setembro de 2009 20:04
  • 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.
    domingo, 20 de setembro de 2009 20:09
  • There is none.  You don't need one, just don't set the InitialDirectory property and it is automatic.

    Hans Passant.
    domingo, 20 de setembro de 2009 20:20
  • 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.
    domingo, 20 de setembro de 2009 20:31
  • 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
    • Sugerido como Resposta CarlosMartini domingo, 20 de fevereiro de 2011 00:53
    • Não Sugerido como Resposta CarlosMartini domingo, 20 de fevereiro de 2011 00:53
    domingo, 20 de setembro de 2009 20:34

    • 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
    • Sugerido como Resposta CarlosMartini domingo, 20 de fevereiro de 2011 01:00
    domingo, 20 de fevereiro de 2011 00:58
  • Are you just completely stupid? The OP asked a question about retrieving a value and nothing you have posted answers that question/ If the OP "didn't need one", then they wouldn't have asked how to get one. As their reply clearly states... that actually do.

    Try answering the question instead of getting your buddies to upvote your useless answers in future, the question is more important than your ego. Sad that people like you get to be moderators of these sites, when you add nothing of value to the questions being asked.


    Worms Forts 10/10, MotoGP3 10/10, Worms 10/10, Rogue Trooper 10/10 - Thank you Total Mobile :D

    terça-feira, 9 de junho de 2020 13:20
  • And another idiot joins the playfield.

    OP asks "How do I get the value of the last path used?", meaning they don't know how to get the last path.

    Your suggestion "Keep the value of the last path used...". I mean seriously, do you even read anything that is posted? How can they keep a value they don't know how to get?


    Worms Forts 10/10, MotoGP3 10/10, Worms 10/10, Rogue Trooper 10/10 - Thank you Total Mobile :D

    terça-feira, 9 de junho de 2020 13:24
  • Thank you for being the only useful person to have taken part in this question and to have actually provided a relevant answer.

    No surprise that Mr Moderator gets his useless answer proposed as correct and gets to keep it. There should be more people like you on here and no people like him. Again, thank you!


    Worms Forts 10/10, MotoGP3 10/10, Worms 10/10, Rogue Trooper 10/10 - Thank you Total Mobile :D

    terça-feira, 9 de junho de 2020 13:28