Microsoft Developer Network > Forenhomepage > Windows Presentation Foundation (WPF) > How do I get the path lastly accessed like OpenFileDialog does?
Stellen Sie eine FrageStellen Sie eine Frage
 

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

  • Freitag, 3. Juli 2009 00:10JaeHyeok14 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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

Antworten

Alle Antworten

  • Freitag, 3. Juli 2009 01:01Mark SalsberyMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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++
  • Freitag, 3. Juli 2009 08:00Tim DawsonMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Samstag, 4. Juli 2009 01:58JaeHyeok14 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Samstag, 4. Juli 2009 20:48Kenneth Haugland TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     

    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
  • Montag, 6. Juli 2009 16:55JaeHyeok14 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Montag, 6. Juli 2009 22:27Kenneth Haugland TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Montag, 6. Juli 2009 23:14JaeHyeok14 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Dienstag, 7. Juli 2009 08:59Tim DawsonMVPTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
    • Nicht als Antwort vorgeschlagenJaeHyeok14 Mittwoch, 8. Juli 2009 22:29
    • Als Antwort vorgeschlagenKenneth Haugland Dienstag, 7. Juli 2009 09:19
    •  
  • Mittwoch, 8. Juli 2009 21:28JaeHyeok14 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Enthält Code
    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
  • Mittwoch, 8. Juli 2009 22:01Kenneth Haugland TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    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
  • Mittwoch, 8. Juli 2009 22:10Kenneth Haugland TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     Beantwortet
    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
  • Mittwoch, 8. Juli 2009 22:29JaeHyeok14 TeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillenTeilnehmermedaillen
     
    Thank you Ken!!
    Jae