How to list directories / files of a given path in your listbox easily?
![]()
Assume you’ve a listbox and you want to fill all directories/files of a particular path to it. The usual method is iterate each file by FindFirstFile() and FindNextFile(), then add to listbox. Or any other cool tricks to do the same more easily?
![]()
You can use the api – DlgDirList(). It will populate the directories or files or drives to your listbox. You can select it by providing appropriate options. See the sample code snippet below, which lists all directories under C:\.
// The path Variable should be modifiable buffer. // Since DlgDirList() returns data through it. TCHAR Path[MAX_PATH] = _T("C:\\"); ::DlgDirList( GetSafeHwnd(), Path, // in/out param. IDC_LIST1, 0, DDL_DIRECTORY | DDL_EXCLUSIVE );
For identifying directories, the DlgDirList() will decorate the directory name in format “[Directory]“. Now how to get the selected directory path from the listbox? Since its decorated, for that purpose, you can use function – DlgDirSelectEx(). It will undecorated the directory name and returns the real name. See the code snippet below.
// Read the selected directory from Listbox
TCHAR SelectedPath[MAX_PATH] = { 0 };
DlgDirSelectEx( GetSafeHwnd(),
SelectedPath,
MAX_PATH,
IDC_LIST1 );
![]()
Have a look at similar apis, such as DlgDirListComboBox(), DlgDirListComboBoxEx() etc. The name itself explains about it. Isn’t it?
![]()
Targeted Audience – Intermediate.
Thanks dear
Thanks everyone for the great insight and post here.Its great when everyone can share ideas and expirence with others and when it makes a differnce as well.i will be making a contribution here as well.thanks
Hi Jeff,
Thanks a lot for sharing your idea. At present, weseetips is a personal blog, where i share my ideas. But It will be great, if other can take part and contribute. I’m thinking about it seriously, may be soon, weseetips will be open to public contribution. Again thanks a lot for the suggestion.
Regards,
Jijo.
It is best to participate in a contest for probably the greatest blogs on the web. I will suggest this web site!