/*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_HIDDEN
winnt.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