Microsoft Developer Network > Página principal de foros > Windows Presentation Foundation (WPF) > How do I get the path lastly accessed like OpenFileDialog does?
Formular una preguntaFormular una pregunta
 

RespondidaHow do I get the path lastly accessed like OpenFileDialog does?

  • viernes, 03 de julio de 2009 0:10JaeHyeok14 Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Hi all,

    I'm using both OpenFileDialog and FolderBrowserDialog in my app.
    I found OpenFileDialog always remembers the directory path lastly accessed 
    (I'm not setting InitialDirectory property), while FolderBrowserDialog does not.
    How do I get this path? I would like to open FolderBrowserDialog with this path set.

    Many thanks.




    Jae

Respuestas

  • miércoles, 08 de julio de 2009 22:10Kenneth Haugland Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Hey guys... This is a direct copy from a reply from Nobugs (Hans Pessant)

    Yes, if you do not set the InitialDirectory property, OpenFileDialog looks in the registry for the last path that was used to open a file.  I think the key is HKCU\ Software\ Microsoft\ Windows\ CurrentVersion\ Explorer\ Comdlg32\ OpenSaveMRU.  You shouldn't rely on this history but it is convenient to the user if you can't provide a decent InitialDirectory value.

    http://social.msdn.microsoft.com/forums/en-US/winforms/thread/94b94dc2-0460-4a1e-bdf6-7d3318b9c8ba


    btw he is almost right it should be HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU



    Kenneth
    • Propuesto como respuestaKenneth Haugland miércoles, 08 de julio de 2009 22:15
    • Marcado como respuestaJaeHyeok14 miércoles, 08 de julio de 2009 22:28
    •  

Todas las respuestas

  • viernes, 03 de julio de 2009 1:01Mark SalsberyMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    As far as I know, you'll need to track the last selected folder yourself.

    You can use the FolderBrowserDialog.RootFolder and/or FolderBrowserDialog.SelectedPath properties to set the folder that will be initially selected when the dialog is opened.


    Mark

    Mark Salsbery Microsoft MVP - Visual C++
  • viernes, 03 de julio de 2009 8:00Tim DawsonMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    OpenFileDialog doesn't explicitly remember; it just sets the Environment.CurrentDirectory and uses the current directory when it's next opened. So, if you use that when you display your FolderBrowserDialog, that should achieve the same thing.
    Controls for WPF, Windows Forms and Silverlight at http://www.divelements.co.uk
  • sábado, 04 de julio de 2009 1:58JaeHyeok14 Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Thank you for your reply.

    It seems, however, Environment.CurrentDirectory is always the directory where my executable is running (Debug folder). 
    Any idea?

    Thanks again!



    Jae
  • sábado, 04 de julio de 2009 20:48Kenneth Haugland Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     

    Are you draging or opening the file for the first time in your application? If you last opend you program its not so suprising....

    Cant you store a list of open folders before you run your application? That will requre that a small application will be running all the time though....


    Kenneth
  • lunes, 06 de julio de 2009 16:55JaeHyeok14 Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Hi Ken,

    Thanks for your response. My application doesn't open any files initially, but user may (or may not) open OpenFileDialog or FolderBrowserDialog any time from the application.

    Storing (in file or registry) the folder path is a workaround here but I just want to retrieve whatever OpenFileDialog is using, since it already knows what it is.

    Thanks!

    Jae
  • lunes, 06 de julio de 2009 22:27Kenneth Haugland Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    OpenFileDialog doesn't explicitly remember; it just sets the Environment.CurrentDirectory and uses the current directory when it's next opened. So, if you use that when you display your FolderBrowserDialog, that should achieve the same thing.
    Controls for WPF, Windows Forms and Silverlight at http://www.divelements.co.uk

    Hmm.. i dont understand this completely.... Ie when the OpenFileDialog opens a file... Does it store the Enviroment.CurrentDirectory in some String?

    it then must stor it somewhere... Or am I complitely on the wrong track here?

    Kenneth
  • lunes, 06 de julio de 2009 23:14JaeHyeok14 Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Hi Ken,

    Thanks again for your response.
    You are right. It seems OpenFileDialog stores it somewhere else than Environment.CurrentDirectory. I hope to know where it is. :P

    Jae
  • martes, 07 de julio de 2009 8:59Tim DawsonMVPMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    I can't believe this thread is still going. Use the code below. You will see that once a file has been picked from the OpenFileDialog, the current directory has changed. As I said before, this is how it appears to "remember" the last folder used.

     

    OpenFileDialog d = new OpenFileDialog();

    d.ShowDialog(

    this);

    label1.Content =

    Environment.CurrentDirectory;


    Controls for WPF, Windows Forms and Silverlight at http://www.divelements.co.uk
    • Votado como útilJaeHyeok14 miércoles, 08 de julio de 2009 22:29
    • Propuesto como respuestaKenneth Haugland martes, 07 de julio de 2009 9:19
    •  
  • miércoles, 08 de julio de 2009 21:28JaeHyeok14 Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Tiene código
    Hi Tim,

    Environment.CurrentDirectory is initially pointing to the working directory, not the lastly accessed folder path.
    It is true that Environment.CurrentDirectory is set to whatever the directory selected by OpenFileDialog when it returns DialogResult.OK, but, before OpenFIleDialog is opened, it is still the working directory. 

    What I mean by 'remember' is that OpenFileDialog knows lastly accessed folder when Environment.CurrentDirectory is still pointing to the working directory.

    So..

    label1.Content = Environment.CurrentDirectory; // working directory
    
    OpenFileDialog d = new OpenFileDialog(); // d opens with lastly selected directory
    
    if(DialogResult.OK == d.ShowDialog(this))
    {
    label1.Content = Environment.CurrentDirectory; // selected directory
    }
    else
    {
    label2.Content = Environment.CurrentDirectory; // working directory
    }
    

     




    Jae
    • EditadoJaeHyeok14 miércoles, 08 de julio de 2009 21:35
    • EditadoJaeHyeok14 miércoles, 08 de julio de 2009 21:34
    •  
  • miércoles, 08 de julio de 2009 22:01Kenneth Haugland Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Hi Jae

    I gave up and maked a proposed answer... sorry...

    But... I think you are right...  and I personaly thinks its stored in the registery  some place ...
    Kenneth
  • miércoles, 08 de julio de 2009 22:10Kenneth Haugland Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     Respondida
    Hey guys... This is a direct copy from a reply from Nobugs (Hans Pessant)

    Yes, if you do not set the InitialDirectory property, OpenFileDialog looks in the registry for the last path that was used to open a file.  I think the key is HKCU\ Software\ Microsoft\ Windows\ CurrentVersion\ Explorer\ Comdlg32\ OpenSaveMRU.  You shouldn't rely on this history but it is convenient to the user if you can't provide a decent InitialDirectory value.

    http://social.msdn.microsoft.com/forums/en-US/winforms/thread/94b94dc2-0460-4a1e-bdf6-7d3318b9c8ba


    btw he is almost right it should be HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32\OpenSavePidlMRU



    Kenneth
    • Propuesto como respuestaKenneth Haugland miércoles, 08 de julio de 2009 22:15
    • Marcado como respuestaJaeHyeok14 miércoles, 08 de julio de 2009 22:28
    •  
  • miércoles, 08 de julio de 2009 22:29JaeHyeok14 Medallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuarioMedallas del usuario
     
    Thank you Ken!!
    Jae