1、打开目录
常用于目录选择
CString SelectPath()
{
char filename[255];
BROWSEINFO info;
info.hwndOwner = NULL ;
info.pidlRoot = (LPCITEMIDLIST)NULL;
info.pszDisplayName = filename;
info.lpszTitle = "请选择文件夹:";
info.ulFlags = BIF_BROWSEFORCOMPUTER | BIF_RETURNONLYFSDIRS ;
info.lpfn = NULL;
info.lParam = 0;
info.iImage = 0 ;
LPITEMIDLIST idlist = SHBrowseForFolder(&info);
char szpath[255];
SHGetPathFromIDList(idlist,szpath);
return szpath;
}
2、打开文件
常用于文件选择
CFileDialog dlg(true, NULL, NULL, NULL, "*.txt|*.txt||");
if(dlg.DoModal() == IDOK)
{
CString strFile = dlg.GetPathName();
}
3、遍历目录
采用递归方式,输出文件名称和路径
#include "windows.h"
void BrowseFile(string& strFile)
{
int nRet =0;
string dirTemp;
string strTemp;
WIN32_FIND_DATA FindFileData;
HANDLE hFind =INVALID_HANDLE_VALUE;
string strDir = strFile;
nRet = strDir.find_last_of("\\");
if(nRet strDir+="\\*.*";
else
strDir+="*.*";
hFind = FindFirstFile((char*)strDir.c_str(), &FindFileData);
if (hFind == INVALID_HANDLE_VALUE)
return ;
while (FindNextFile(hFind, &FindFileData))
{
if(FindFileData.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
if((strcmp(FindFileData.cFileName, ".") == 0)|| (strcmp(FindFileData.cFileName, "..") == 0))
continue;
dirTemp=strFile;
dirTemp=dirTemp+"\\";
dirTemp+= FindFileData.cFileName;
BrowseFile(dirTemp);//递归调用
}
else //到达最低层的文件
{
strTemp=strFile;
strTemp=strFile+"\\";
strTemp+=FindFileData.cFileName;
nRet = FindStr(strFileName);
cout<
}
}
FindClose(hFind);
}
阅读(1731) | 评论(0) | 转发(0) |