MSDN > 論壇首頁 > Windows Presentation Foundation (WPF) > How do I get the path lastly accessed like OpenFileDialog does?
發問發問
 

已答覆How do I get the path lastly accessed like OpenFileDialog does?

  • Friday, 3 July, 2009 0:10JaeHyeok14 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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

解答

  • Wednesday, 8 July, 2009 22:10Kenneth Haugland 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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

所有回覆

  • Friday, 3 July, 2009 1:01Mark SalsberyMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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++
  • Friday, 3 July, 2009 8:00Tim DawsonMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • Saturday, 4 July, 2009 1:58JaeHyeok14 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • Saturday, 4 July, 2009 20:48Kenneth Haugland 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     

    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
  • Monday, 6 July, 2009 16:55JaeHyeok14 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • Monday, 6 July, 2009 22:27Kenneth Haugland 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • Monday, 6 July, 2009 23:14JaeHyeok14 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • Tuesday, 7 July, 2009 8:59Tim DawsonMVP使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • Wednesday, 8 July, 2009 21:28JaeHyeok14 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     包含代碼
    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
  • Wednesday, 8 July, 2009 22:01Kenneth Haugland 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    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
  • Wednesday, 8 July, 2009 22:10Kenneth Haugland 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     已答覆
    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
  • Wednesday, 8 July, 2009 22:29JaeHyeok14 使用者勳章使用者勳章使用者勳章使用者勳章使用者勳章
     
    Thank you Ken!!
    Jae