I\'m making an application with wxWidgets that has a listbox in it. I want to get the current working directory of the application, and in that listbox, list all the folders
For getting only the subdirectories without recursion the answer is right in the documentation of the wxDir class: http://docs.wxwidgets.org/trunk/classwx_dir.html
wxDir dir("C:/myDir");
if(!dir.IsOpened())
{
// deal with the error here, wxDir would already
// log an error message explaining the exact reason of the failure.
return;
}
wxString filename;
bool cont = dir.GetFirst(&filename, wxEmptyString, wxDIR_DIRS);
while(cont)
{
printf("%s\n", filename.c_str());
cont = dir.GetNext(&filename);
}
For recursion i use a Traverse sub-class: http://docs.wxwidgets.org/trunk/classwx_dir_traverser.html
The trick is only add to the list what you need, this is a case for directories only:
class wxDirTraverserSimple : public wxDirTraverser
{
public:
wxDirTraverserSimple(wxArrayString& files) : m_files(files){}
virtual wxDirTraverseResult OnFile(const wxString& filename)
{
return wxDIR_CONTINUE;
}
virtual wxDirTraverseResult OnDir(const wxString& dirname)
{
m_files.Add(dirname);
return wxDIR_CONTINUE;
}
private:
wxArrayString& m_files;
};