MFC获取文件夹下的所有文件名

1、获取路径:通过CFileDialog获取文件夹的路径,以及文件夹下面的一个文件


OnBnClickedBtnOpenfile()
{
	// TODO: Add your control notification handler code here	
	CFileDialog dlg(TRUE);///TRUE为OPEN对话框,FALSE为SAVE AS对话框
	CString csDirParth;
	if(dlg.DoModal()==IDOK)
		m_csFileName = dlg.GetPathName();
	else
		m_csFileName.Empty();

	int iEndPos = 0;
	iEndPos = m_csFileName.ReverseFind('\\');
	csDirParth = m_csFileName.Left(iEndPos);

	m_FileList.clear();
	GetFileFromDir(csDirParth);
}


2、下面的代码可以获取路径csDirPath下的txt文件,并将所有的txt文件名保存在vector<CString>类型的变量m_FileList中;

GetFileFromDir(CString csDirPath)
{
	csDirPath+="\\*.txt";
	HANDLE file;
	WIN32_FIND_DATA fileData;
	char line[1024];
	char fn[1000];
	//mbstowcs(fn,csDirPath.GetBuffer(),999);
	file = FindFirstFile(csDirPath.GetBuffer(), &fileData);
	m_FileList.push_back(fileData.cFileName);
	bool bState = false;
	bState = FindNextFile(file, &fileData);
	while(bState){
		//wcstombs(line,(const char*)fileData.cFileName,259);
		m_FileList.push_back(fileData.cFileName);
		bState = FindNextFile(file, &fileData);
	}
}


你可能感兴趣的:(MFC获取文件夹下的所有文件名)