天天看点

MFC中打开一个文件夹(非文件)并读取文件夹中所有文件的路径

```cpp
//读取文件夹下的所有文件路径
void getFiles(CString path, vector<CString>& files)
{
	CFileFind find;
	BOOL IsFind = find.FindFile(path + _T("/*.*"));
	while (IsFind)
	{
		IsFind = find.FindNextFile();
		if (find.IsDots())
		{
			continue;
		}
		else
		{
			CString filename = _T("");
			CString fullname = _T("");
			filename = find.GetFileName();
			fullname = path + filename;
			files.push_back(fullname);
		}
	}
}
//打开一个文件夹
void OnBnClickedButton1()
{
	vector<CString>listFile;
	listFile.clear();
	CString m_saveFilePath;
	TCHAR szPath[_MAX_PATH];
	BROWSEINFO bi;
	bi.hwndOwner = GetSafeHwnd();
	bi.pidlRoot = NULL;
	bi.lpszTitle = "Please select the save path";
	bi.pszDisplayName = szPath;
	bi.ulFlags = BIF_RETURNONLYFSDIRS;
	bi.lpfn = NULL;
	bi.lParam = NULL;
	LPITEMIDLIST pItemIDList = SHBrowseForFolder(&bi);
	if (pItemIDList)
	{
		if (SHGetPathFromIDList(pItemIDList, szPath))
		{
			m_saveFilePath = szPath;
			m_saveFilePath += "\\";
		}
		IMalloc* pMalloc;
		if (SHGetMalloc(&pMalloc) != NOERROR)
		{
			TRACE(_T("Can't get the IMalloc interface\n"));
		}
		pMalloc->Free(pItemIDList);
		if (pMalloc)
		{
			pMalloc->Release();
		}
		UpdateData(false);
	}
	if(m_saveFilePath!="")
		getFiles(m_saveFilePath.GetBuffer(0), listFile);  //保存文件夹中的文件路径到listFile
}