//VS2015,
//CListCtrl初始化。注意该控件的View属性需要设置为Report!!!!
BOOL CloadfileServerDlg::OnInitDialog()
{
......
pListCtr.DeleteAllItems(); pListCtr.InsertColumn(0, _T("文件名"), LVCFMT_CENTER, 120); pListCtr.InsertColumn(1, _T("文件类型"), LVCFMT_CENTER, 100); pListCtr.InsertColumn(2, _T("文件大小"), LVCFMT_CENTER, 140); pListCtr.InsertColumn(3, _T("存放路径"), LVCFMT_LEFT, 500);
......
}
//列表出文件 void CloadfileServerDlg::getFileList(char* szPath)//参数为文件夹路径,如:"C:\Users\ForeverRoad\Desktop\serverImg" { pListCtr.DeleteAllItems(); CFileFind finder; //建立CString对象,设置检索匹配字符串 CString strWildcard(szPath); strWildcard += _T("\*.*"); //文件内部检索 BOOL bWorking = finder.FindFile(strWildcard); int i; //用于定位字符,作为下标 int index = 0; //用于列表索引 CString fontName, lastType, filepath,filename,filesize;//存储切割结果 while (bWorking) { bWorking = finder.FindNextFile(); filename = finder.GetFileName();
//假设文件夹里面全是图片,获取图片文件长度 imageSize = finder.GetLength(); if (filename != _T(".") && filename != _T("..")) { filepath = finder.GetFilePath(); for (i = 0;i < filename.GetLength();i++) { if (filename[i] == '.') { fontName = filename.Mid(0, i); lastType = filename.Mid(i+1, filename.GetLength()); filesize.Format(_T("%d"), imageSize/1024); filesize += "k"; pListCtr.InsertItem(index, fontName); pListCtr.SetItemText(index, 1, lastType); pListCtr.SetItemText(index, 2, filesize); pListCtr.SetItemText(index, 3, filepath); } } index++; } } finder.Close(); }
文件夹内部内容,从cmd看:
程序运行结果: