标签:
void C文件Dlg::OnBnClickedButton1()
{
	// TODO: 在此添加控件通知处理程序代码
	CString strFolderPath(_T(""));
	TCHAR szPath[_MAX_PATH];
	BROWSEINFO bi;
	bi.hwndOwner = GetSafeHwnd();
	bi.pidlRoot = NULL;
	bi.lpszTitle = _T("选择文件夹");
	bi.pszDisplayName = szPath;
	bi.ulFlags = BIF_RETURNONLYFSDIRS;
	bi.lpfn = NULL;
	bi.lParam = NULL;
LPITEMIDLIST pItemIDList = SHBrowseForFolder(&bi);
	if(pItemIDList)
	{
		if(SHGetPathFromIDList(pItemIDList,szPath))
		{
			strFolderPath = szPath;
		}
		// 防止内存泄露,要使用IMalloc接口
		IMalloc* pMalloc;
		if( SHGetMalloc(&pMalloc) != NOERROR )
		{
			TRACE(_T("无法取得外壳程序的IMalloc接口\n"));
		}
		pMalloc->Free(pItemIDList);
		if(pMalloc)
			pMalloc->Release();
	}
	else
	{
		strFolderPath = _T("");    // 文件夹路径为空
	}
ScanDiskFile(strFolderPath);
}
void C文件Dlg::ScanDiskFile(const CString& strPath)
{
	CFileFind find;
	CString strTemp = strPath;
	CString strDirectory = strPath + _T("\\") + _T("\\*.*");
	CString strFile;
vector<CString> m_vStrAllFiles; // zm容器
	BOOL IsFind = find.FindFile(strDirectory);
	while(IsFind )
	{
		IsFind=find.FindNextFile();
		// 如果是"." 则不扫描
		if(find.IsDots())
			continue;
		// 如果是是目录,继续扫描此目录
		else if(find.IsDirectory())
		{
			strFile = find.GetFileName();
			strTemp = strTemp + _T("\\") + strFile;
			ScanDiskFile(strTemp);
		}
		// 文件
		else
		{            
			strFile = find.GetFileName();
// 此处也可以根据相应的扩展名来设置
// ......
			m_vStrAllFiles.push_back(strFile);
		}
	}
find.Close();
//以下这三行是我测试用的,你可以把它们删掉
	CString temp;
	//temp.Format("i = %d", m_vStrAllFiles.size());
	MessageBox(temp);	
}
标签:
原文地址:http://www.cnblogs.com/zzh123/p/4797294.html