[英]wxWidgets - Write all folders into a listbox
I know there's a thread with a similar question but it doesn't work the way it's supposed to be there. 我知道有一个类似问题的线程 ,但是它不应该按预期的方式工作。 I'm fairly new to c++ and wxWidgets so please as easy as possible.
我对c ++和wxWidgets还是很陌生,所以请尽可能简单。
void dlgMain::getAllDirectories(wxString Path)
{
wxDir dir(Path);
wxString dirName = dir.GetName();
wxArrayString dirList;
dir.GetAllFiles(dirName, &dirList, wxEmptyString, wxDIR_DIRS | wxDIR_FILES);
m_lbDir->Clear();
for (int i = 0; i < dirList.size(); i++)
{
//wxMessageBox(dirList[i].c_str());
m_lbDir->Append(dirList[i].c_str());
}
}
Path contains the path to a directory(ie "C:\\Folder1\\"). 路径包含目录的路径(即“ C:\\ Folder1 \\”)。 I want to list all folders(not files) within Folder1 into my listbox.
我想将Folder1内的所有文件夹(而非文件)列出到我的列表框中。 My Problem is that it doesn't work with GetAllFiles() the way I want to.
我的问题是,它不能按我想要的方式与GetAllFiles()一起使用。 It gives back all directories, subdirectories and files and lists them with their full path.
它返回所有目录,子目录和文件,并列出其完整路径。 I've tried using just wxDIR_DIRS as a Filter but that won't return anything?
我已经尝试过仅使用wxDIR_DIRS作为过滤器,但是不会返回任何内容? Any ideas?
有任何想法吗?
If you only want to get the directories, and not the sub-directories or files, then you can create a class derived from wxDirTraverser
to do that as follows: 如果只想获取目录,而不是子目录或文件,则可以创建一个从
wxDirTraverser
派生的类,以执行以下操作:
#include <wx/dir.h>
class wxDirectoriesEnumerator : public wxDirTraverser {
public:
wxArrayString *dirs;
wxDirectoriesEnumerator(wxArrayString* dirs_) {
dirs=dirs_;
}
//This function will be called when a file is found
virtual wxDirTraverseResult OnFile(const wxString& filename) {
//Do nothing, continue with the next file or directory
return wxDIR_CONTINUE;
}
//This function will be called when a directory is found
virtual wxDirTraverseResult OnDir(const wxString& dirname) {
//Add the directory to the results
dirs->Add(dirname);
//Do NOT enter this directory
return wxDIR_IGNORE;
}
};
You can then use it as follows: 然后可以按以下方式使用它:
wxArrayString dirList;
wxDirectoriesEnumerator traverser(&dirList);
wxDir dir("C:\\Folder1");
if (dir.IsOpened()) {
dir.Traverse(traverser);
ListBox1->Clear();
for(unsigned int i=0; i<dirList.GetCount(); i++) {
//The name is what follows the last \ or /
ListBox1->Append(dirList.Item(i).AfterLast('\\').AfterLast('/'));
}
}
I think you'll want to replace ListBox1
with m_lbDir
, if that's the name of your ListBox. 我想您要用
m_lbDir
替换ListBox1
,如果那是您的ListBox的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.