MFC遍历文件夹(遍历目录),遍历文件
/*Title:MFC遍历文件夹(遍历目录),遍历文件
*Author:Insun
*Blog:http://yxmhero1989.blog.163.com
*From:http://www.4safer.com/forum.php?mod=viewthread&tid=634&page=1&extra=#pid1915
*/
Main Directory用Combox,ID:IDC_MAIN_DIR,右键之,添加变量:类别Control;变量名:m_cbMainDir
Sub Directory用ListBox,ID:IDC_SUB_DIRS,右键之,添加变量:类别Control;变量名:m_lbSubDirs
All files用ListBox,ID:IDC_LIST_ALL_FILES,右键之,添加变量:类别Contr;变量名:m_strAllFiles在OnInitDialog() 初始化函数里面最后添加如下代码:
//--------------------------------------------------
//char *driver[] = { "A:", "C:", "D:", "E:", ...... };
char szBuffer[MAX_PATH];
m_cbMainDir.AddString("C:"); //自己添加驱动器算了,哎- -
GetWindowsDirectory(szBuffer,MAX_PATH);//Windows目录
m_cbMainDir.AddString(szBuffer);
szBuffer[2]=0;//驱动器 C: 第3个去掉
m_cbMainDir.AddString(szBuffer);
GetSystemDirectory(szBuffer,MAX_PATH);//系统目录
m_cbMainDir.AddString(szBuffer);
GetCurrentDirectory(MAX_PATH,szBuffer);//当前目录
m_cbMainDir.AddString(szBuffer);
//--------------------------------------------------都有注释,看起来简单,3个函数而已。
在OnCbnSelchangeMainDir()函数如下:
void CLesson22Dlg::OnCbnSelchangeMainDir()
{
CString strMainDir,strPath;
int nSelection;
HANDLE hFind;
WIN32_FIND_DATA dataFind;
BOOL bMoreFiles;nSelection = m_cbMainDir.GetCurSel(); //check the index is valid
if(nSelection !=CB_ERR)
{
m_cbMainDir.GetLBText(nSelection,strMainDir);
m_lbSubDirs.ResetContent();//clean
strPath = strMainDir + "\*.*";
hFind = FindFirstFile(strPath,&dataFind);
bMoreFiles = true;
while(hFind != INVALID_HANDLE_VALUE && bMoreFiles == TRUE)
{
//if(dataFind.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)//Directories
if((dataFind.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
if(strcmp(dataFind.cFileName ,"."))
if(strcmp(dataFind.cFileName,".."))// ignored the "." and ".." 2 special directory entries
{
m_lbSubDirs.AddString(dataFind.cFileName);
}
}
if (dataFind.dwFileAttributes == FILE_ATTRIBUTE_ARCHIVE) //ARCHIVE存档;HIDDEN;READONLY
{
m_strAllFiles.AddString(dataFind.cFileName);
}
if ((dataFind.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
//||(dataFind.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN)
||(dataFind.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
{ //非要显示隐藏文件和系统文件和只读文件
m_strAllFiles.AddString(dataFind.cFileName);
}
bMoreFiles = FindNextFile(hFind,&dataFind);}
FindClose(hFind);// close the handle
}其实仔细研究起来就难了。
一般一个目录或文件有三个属性:只读、隐藏、存档。
dataFind.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY
||dataFind.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY+
FILE_ATTRIBUTE_READONLY
||dataFind.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY+
FILE_ATTRIBUTE_ARCHIVE
||dataFind.dwFileAttributes==FILE_ATTRIBUTE_DIRECTORY+
FILE_ATTRIBUTE_HIDDENwinnt.h里面定义了很多:
#define FILE_SHARE_READ 0x00000001
#define FILE_SHARE_WRITE 0x00000002
#define FILE_SHARE_DELETE 0x00000004
#define FILE_ATTRIBUTE_READONLY 0x00000001
#define FILE_ATTRIBUTE_HIDDEN 0x00000002
#define FILE_ATTRIBUTE_SYSTEM 0x00000004
#define FILE_ATTRIBUTE_DIRECTORY 0x00000010
#define FILE_ATTRIBUTE_ARCHIVE 0x00000020
#define FILE_ATTRIBUTE_DEVICE 0x00000040
#define FILE_ATTRIBUTE_NORMAL 0x00000080
#define FILE_ATTRIBUTE_TEMPORARY 0x00000100
#define FILE_ATTRIBUTE_SPARSE_FILE 0x00000200
#define FILE_ATTRIBUTE_REPARSE_POINT 0x00000400
#define FILE_ATTRIBUTE_COMPRESSED 0x00000800
#define FILE_ATTRIBUTE_OFFLINE 0x00001000
#define FILE_ATTRIBUTE_NOT_CONTENT_INDEXED 0x00002000
#define FILE_ATTRIBUTE_ENCRYPTED 0x00004000
#define FILE_NOTIFY_CHANGE_FILE_NAME 0x00000001
#define FILE_NOTIFY_CHANGE_DIR_NAME 0x00000002
#define FILE_NOTIFY_CHANGE_ATTRIBUTES 0x00000004
#define FILE_NOTIFY_CHANGE_SIZE 0x00000008
#define FILE_NOTIFY_CHANGE_LAST_WRITE 0x00000010
#define FILE_NOTIFY_CHANGE_LAST_ACCESS 0x00000020
#define FILE_NOTIFY_CHANGE_CREATION 0x00000040
#define FILE_NOTIFY_CHANGE_SECURITY 0x00000100
#define FILE_ACTION_ADDED 0x00000001
#define FILE_ACTION_REMOVED 0x00000002
#define FILE_ACTION_MODIFIED 0x00000003
#define FILE_ACTION_RENAMED_OLD_NAME 0x00000004
#define FILE_ACTION_RENAMED_NEW_NAME 0x00000005
#define MAILSLOT_NO_MESSAGE &nb
补充:软件开发 , C语言 ,