1.MFC获取文件夹路径信息
[cpp] view plaincopy
CString GetPath()
{
CString strPath = "";
BROWSEINFO bInfo;
ZeroMemory(&bInfo, sizeof(bInfo));
bInfo.hwndOwner = m_hWnd;
bInfo.lpszTitle = _T("请选择路径: ");
bInfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_EDITBOX;
LPITEMIDLIST lpDlist; //用来保存返回信息的IDList
lpDlist = SHBrowseForFolder(&bInfo) ; //显示选择对话框
if(lpDlist != NULL) //用户按了确定按钮
{
TCHAR chPath[MAX_PATH]; //用来存储路径的字符串
SHGetPathFromIDList(lpDlist, chPath);//把项目标识列表转化成字符串
strPath = chPath; //将TCHAR类型的字符串转换为CString类型的字符串
}
return strPath;
}
2.获取文件夹下指定格式的文件,用CListCtrl控件输出其文件名及文件大小
[cpp] view plaincopy
void AddFolder(CString csFolder)
{
if (csFolder[csFolder.GetLength() - 1] != '\\') csFolder += _T("\\");
csFolder += _T("*.*");
CFileFind ff;
BOOL bFind = ff.FindFile(csFolder);
while (bFind)
{
bFind = ff.FindNextFile();
if (ff.IsDots()) continue;
if (ff.IsDirectory())
{
AddFolder(ff.GetFilePath());
continue;
}
CString csFile = ff.GetFilePath();
csFile.MakeLower();
int nFileType = GetFileType(csFile);
if (nFileType == -1) continue;
LVFINDINFO info;
info.flags = LVFI_STRING;
info.psz = csFile;
if (m_cFiles.FindItem(&info, -1) < 0)
m_cFiles.InsertItem(0, (LPCTSTR)csFile);
double size = (double)ff.GetLength()/1024.0f;
CString strFileSize;
strFileSize.Format(_T("%.1lf"), size);
m_cFiles.SetItemText(m_cFiles.FindItem(&info, -1), 3, (LPCTSTR)strFileSize);
}
}
[cpp] view plaincopy
int GetFileType(const CString &csFile) const
{
int nType = -1;
CString csExt = csFile.Right(4);
if (csExt == _T(".doc"))
nType = 0x0101;
else if (csExt == _T(".rtf"))
nType = 0x0103;
else if (csExt == _T(".xls"))
nType = 0x0201;
else if (csExt == _T(".ppt"))
nType = 0x0301;
if (nType == -1)
{
csExt = csFile.Right(5);
if (csExt == _T(".docx"))
nType = 0x0102;
else if (csExt == _T(".xlsx"))
nType = 0x0202;
else if (csExt == _T(".pptx"))
nType = 0x0302;
}
return nType;
}