来自:http://www.fourants.com/blog/129.html
头文件:
#include <QDir> #include <QFile> #include <QString>
示例程序:
//递归遍历文件夹,找到所有的dll文件,调用registerComFile(QString dllfilepath)函数 //sComFilePath:要遍历的文件夹的文件名 int PSM_ComRegister::searchDllFile(const QString& sComFilePath){ QDir dir(sComFilePath); if (!dir.exists()) { return -1; } //取到所有的文件和文件名,但是去掉.和..的文件夹(这是QT默认有的) dir.setFilter(QDir::Dirs|QDir::Files|QDir::NoDotAndDotDot); //文件夹优先 dir.setSorting(QDir::DirsFirst); //转化成一个list QFileInfoList list = dir.entryInfoList(); if(list.size()< 1 ) { return -1; } int i=0; //递归算法的核心部分 do{ QFileInfo fileInfo = list.at(i); QString tt = fileInfo.fileName(); //如果是文件夹,递归 bool bisDir = fileInfo.isDir(); if(bisDir) { searchDllFile(fileInfo.filePath()); } else{ bool isDll = fileInfo.fileName().endsWith(".dll"); if (isDll == true) { registerComFile(fileInfo.filePath()); } }//end else i++; } while(i < list.size()); }
来自:
http://www.fourants.com/blog/129.html