1、选择目录对话框:szPath为选择目录路径
char szPath[MAX_PATH]; //存放选择的目录路径
CString str;
ZeroMemory(szPath, sizeof(szPath));
BROWSEINFO bi;
bi.hwndOwner = m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szPath;
bi.lpszTitle = "请选择需要打包的目录:";
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
//弹出选择目录对话框
LPITEMIDLIST lp = SHBrowseForFolder(&bi);
if(lp && SHGetPathFromIDList(lp, szPath))
{
str.Format("选择的目录为 %s", szPath);
AfxMessageBox(str);
}
else
AfxMessageBox("无效的目录,请重新选择");
2、选择文件对话框:strFile为文件路径
CString strFile = _T("");
CFileDialog dlgFile(TRUE, NULL, NULL, OFN_HIDEREADONLY, _T("Describe Files (*.cfg)|*.cfg|All Files (*.*)|*.*||"), NULL);
if (dlgFile.DoModal())
{
strFile = dlgFile.GetPathName();
}
return strFile;
3、获取目录下所有文件名
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile("C:\\*.*", &fd);
if(hFind != INVALID_HANDLE_VALUE)
{
do
{
printf(fd.cFileName);
}
while(::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
4、获取指定目录下某个类型的所有文件个数和名称,弹出对话框显示
char szPath[MAX_PATH]; //存放选择的目录路径
CString str;
ZeroMemory(szPath, sizeof(szPath));
BROWSEINFO bi;
bi.hwndOwner = m_hWnd;
bi.pidlRoot = NULL;
bi.pszDisplayName = szPath;
bi.lpszTitle = "请选择存放文件的目录:";
bi.ulFlags = 0;
bi.lpfn = NULL;
bi.lParam = 0;
bi.iImage = 0;
//弹出选择目录对话框
LPITEMIDLIST lp = SHBrowseForFolder(&bi);
if(lp && SHGetPathFromIDList(lp, szPath))
{
str.Format("%s", szPath);
int f = 0;
CString ff;
CString file[100];
CString add = "";
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(str + "\\*.txt", &fd);
if(hFind != INVALID_HANDLE_VALUE)
{
do
{
file[f] = fd.cFileName;
f++;
}
while(::FindNextFile(hFind, &fd));
for(int i = 0; i < f; i++)
{
add = add + file[i] + "\n";
}
ff.Format("有%d个数据\n",f);
ff+=add;
AfxMessageBox(ff);
::FindClose(hFind);
}
}
else
{
AfxMessageBox("无效的目录,请重新选择");
}