vs2010下MFC按钮打开文件夹并遍历下面所有文件

来源:互联网 发布:机械力学分析软件 编辑:程序博客网 时间:2024/05/29 04:58

vector<CString> files;


CString strFilePath;


遍历路径下的所有文件:

void GetFileFromDir(CString csDirPath)  
{  
csDirPath+="\\*.dcm";  
HANDLE file;  
WIN32_FIND_DATA fileData;  
char line[1024];  
char fn[1000];  
//mbstowcs(fn,csDirPath.GetBuffer(),999);  
file = FindFirstFile(csDirPath.GetBuffer(), &fileData);  
files.push_back(fileData.cFileName);  


CString a;
a = fileData.cFileName;


bool bState = false;  
bState = FindNextFile(file, &fileData);  
while(bState)
{  
//wcstombs(line,(const char*)fileData.cFileName,259);  
files.push_back(fileData.cFileName); 

//MessageBox(a);;
a = fileData.cFileName;


bState = FindNextFile(file, &fileData);  
}  


按钮打开文件夹,并且返回路径:

void CCADPDlg::OnBnClickedSelectfile()
{
// TODO: 在此添加控件通知处理程序代码


LPITEMIDLIST rootLoation;
SHGetSpecialFolderLocation( NULL, CSIDL_DESKTOP, &rootLoation );
if ( rootLoation == NULL ) {
// unkown error
// return
}
// 配置对话框
BROWSEINFO bi;
ZeroMemory( &bi, sizeof( bi ) );
bi.pidlRoot = rootLoation; // 文件夹对话框之根目录,不指定的话则为我的电脑
bi.lpszTitle = _T( "选择DCM文件" ); // 可以不指定
// bi.ulFlags = BIF_EDITBOX | BIF_RETURNONLYFSDIRS;
// 打开对话框, 有点像DoModal
LPITEMIDLIST targetLocation = SHBrowseForFolder( &bi );
TCHAR targetPath[ MAX_PATH ];
if ( targetLocation != NULL )
{

SHGetPathFromIDList( targetLocation, targetPath );
MessageBox( targetPath );
}

strFilePath.Format(_T("%s"),targetPath); 


GetFileFromDir(strFilePath); //遍历所有dcm文件
}

阅读全文
0 0
原创粉丝点击