Making a list of folders in a directory with wxWidgets

前端 未结 3 1150
情歌与酒
情歌与酒 2021-01-14 13:30

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

3条回答
  •  广开言路
    2021-01-14 14:02

    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;
    };
    

提交回复
热议问题