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?
- 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- 編集済みJaeHyeok14 2009年7月3日 0:15
- 編集済みJaeHyeok14 2009年7月3日 0:16
回答
- 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- 回答の候補に設定Kenneth Haugland 2009年7月8日 22:15
- 回答としてマークJaeHyeok14 2009年7月8日 22:28
すべての返信
- 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++ - 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 - 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 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- 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 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- 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 - 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- 回答の候補の設定解除JaeHyeok14 2009年7月8日 22:29
- 回答の候補に設定Kenneth Haugland 2009年7月7日 9:19
- 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- 編集済みJaeHyeok14 2009年7月8日 21:35
- 編集済みJaeHyeok14 2009年7月8日 21:34
- 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 - 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- 回答の候補に設定Kenneth Haugland 2009年7月8日 22:15
- 回答としてマークJaeHyeok14 2009年7月8日 22:28
- Thank you Ken!!
Jae

