Making a list of folders in a directory with wxWidgets

前端 未结 3 1149
情歌与酒
情歌与酒 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:01

    For listing all the folder names in a list,

    #include "wx/dirdlg.h"
    
    wxDirDialog dialog(parent, wxT("Testing directory picker"), cwd, wxDD_NEW_DIR_BUTTON);
    if (dialog.ShowModal() == wxID_OK)
    {
         wxString path = dialog.GetPath();
         wxMessageBox(path);
    }
    

    Note : You can use path in any way you wish, here it just displays in a message box.

    But of course if you want to display folder names in your custom dialog to your user, you can use RyanWilcox solution.

    EDIT: In your code you have not created dirList (or you have posted incomplete code)

    wxArrayString MainWindow::createFolderList()
    {
        wxDir dir(wxGetCwd());
        if ( !dir.IsOpened() )
             ;//handle this error here
    
        wxString dirName = dir.GetName();
        wxArrayString dirList;
    
        dir.GetAllFiles(dirName, &dirList, wxEmptyString, wxDIR_DIRS | wxDIR_FILES);
    
        //Note that next statement is optional
        dirList.shrink();
    
        return dirList;
    }
    

    Your implementation might differ, but I have adapted this code, from my own working code.

    0 讨论(0)
  • 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;
    };
    
    0 讨论(0)
  • 2021-01-14 14:25

    You could use wxDirTraverser - you implement a subclass and override some methods which will be called depending if the item is a file or directory.

    You could also use wxDir::GetAllFiles, which will return a wxArray (which might be more convient for you, displaying it in a list).

    0 讨论(0)
提交回复
热议问题