Click here to Skip to main content
13,045,379 members (123,270 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I wanted to load the browse dialog with previous selection.
on opening the browse dialog it should come with previous selection folder/file..

can any one help me regarding this.
Posted 16-Apr-13 20:06pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

fist of all you can use CFileDialog to do so.:)
for more information on same you may go throught following link of msdn.
Or better refer your msdn.

Now question remain about you need previous directory!!!.
To do so,
CFileDialog open by default CurrentDirectory when it is open.
Means before modal of your CFileDialog if you check
Function you will get one path same path refered by CFileDialog to open.

after that, in any case if you change the current directory you get updated path in CFileDialog.
CurrentDirectory can be change two way.
1)if you call SetCurrentDirectory.
2)And CFileDialog also changes selected path as current directory.

So your solution is that,
1)Never change currentDirectory


2)Better Make on String Variable to store directory path and setDirectory before calling CFileDialog.
Hope it will work for you..

And Yes if you talking about Folder Tree browse dialog then there ,
Not possiblility about setting path.
Because it shows by default Desktop path as currentDirectory.
p.uday kishore 17-Apr-13 2:28am
we created a custom browse dialog for that one side tree control and other list control we used.
now am saving the path also of the current selection.i need to load that into that tree control now...
Coder Block 17-Apr-13 2:32am
Yup then there bit logic.
See you have path that currently selecty ok,
Now you need to set tree node programmically with selected folder that is,
After loading your FolderStruture on tree control(Customize one) just set the node of tree with previously selected folder.
p.uday kishore 17-Apr-13 3:13am
ya am looking on that.not able to set the path to that shell tree control...
Coder Block 17-Apr-13 3:25am
so best article for you is this 1,
perfect match of folder selection..
p.uday kishore 17-Apr-13 5:30am
hey can we compare the node items so we can get that????
Coder Block 17-Apr-13 5:37am
yes you can if you understand following code then your job is much simpler

// name - the name of the item that is searched for
// tree - a reference to the tree control
// hRoot - the handle to the item where the search begins
HTREEITEM FindItem(const CString& name, CTreeCtrl& tree, HTREEITEM hRoot)
// check whether the current item is the searched one
CString text = tree.GetItemText(hRoot);
if (text.Compare(name) == 0)
return hRoot;

// get a handle to the first child item
HTREEITEM hSub = tree.GetChildItem(hRoot);
// iterate as long a new item is found
while (hSub)
// check the children of the current item
HTREEITEM hFound = FindItem(name, tree, hSub);
if (hFound)
return hFound;

// get the next sibling of the current item
hSub = tree.GetNextSiblingItem(hSub);

// return NULL if nothing was found
return NULL;
HTREEITEM CTreeDemoDlg::FindItem(const CString& name, CTreeCtrl& tree)
HTREEITEM root = m_tree.GetRootItem();
while(root != NULL)
HTREEITEM hFound = FindItem(name, tree, root);
if (hFound)
return hFound;

root = tree.GetNextSiblingItem(root);

return NULL;

better undestand code and then used it.
I have alternartive too.With LParam and HParam.
But before that apply above one.

But passing string whould be your searching value..

And one more its function orverwiding too.
May u notice.
p.uday kishore 17-Apr-13 8:48am
i got this code and it is working fine now..
can u suggest me any optimization in this code further.because it is taking more time when a folder contains more files ...

CString defaultPath(_T("My Computer"));
CFile read_object;
CFile cfile_object;
char pbufRead[MAX_PATH];
if (!read_object.Open(csDllPath,CFile::modeRead, &e)) {
if (cfile_object.Open(csDllPath,CFile::modeCreate|CFile::modeReadWrite,&e)) {
cfile_object.Write("My Computer",11);
else {

read_object.Read(pbufRead, sizeof(pbufRead));
CString tStr(pbufRead);

if (pbufRead[1]!=L':') {
HTREEITEM hRootTree=m_CtrlMfcShellTree.GetRootItem();
CString headerTxt=m_CtrlMfcShellTree.GetItemText(hRootTree);
HTREEITEM hChildItem = m_CtrlMfcShellTree.GetNextVisibleItem(hRootTree);
while (hChildItem != NULL) {
hNextItem = m_CtrlMfcShellTree.GetNextItem(hChildItem, TVGN_NEXT);
CString childLabelStr=m_CtrlMfcShellTree.GetItemText(hChildItem);
if (childLabelStr.CompareNoCase(defaultPath)==0)
hChildItem = hNextItem;
else {
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

Try this:
folderBrowser.SelectedPath = "yourpath";

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.170713.1 | Last Updated 17 Apr 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100