返回> 网站首页 

[转载]CFileDialog多选文件路径BUG

yoours2016-04-12 13:27:40 阅读 1707

简介一边听听音乐,一边写写文章。

一、
	CFileDialog OpenDialog(TRUE,NULL,"",OFN_ALLOWMULTISELECT|OFN_ENABLESIZING|OFN_HIDEREADONLY);
	if (OpenDialog.DoModal())
	{
		CString path;
		IShellItemArray *pResult=OpenDialog.GetResults();
		DWORD dwCount=0;
		IShellItem *pItem;
		WCHAR *pFilePath;
		pResult->GetCount(&dwCount);
		for (DWORD i=0;i<dwCount;i++)
		{
			pResult->GetItemAt(i,&pItem);
			pItem->GetDisplayName(SIGDN_FILESYSPATH,&pFilePath);
			path+=pFilePath;
			path+="\n";
		}
		AfxMessageBox(path);
	}

二、
//同时打开N个文件
void COpenNFileDlg::OnButton1() 
{
    CString pathName,fileName,fileTitle;
 
    char* filters = _T("PCM文件(*.pcm)|*.pcm");
 
    //创建一个可以选择多个文件的CFileDialog
    CFileDialog fileDlg(true,NULL,"*.pcm",OFN_ALLOWMULTISELECT | OFN_ENABLESIZING | OFN_HIDEREADONLY,filters);
     
    //最多可以打开500个文件
    fileDlg.m_ofn.nMaxFile = 500 * MAX_PATH;
     
    char* ch = new TCHAR[fileDlg.m_ofn.nMaxFile];
    fileDlg.m_ofn.lpstrFile = ch;
 
    //对内存块清零
    ZeroMemory(fileDlg.m_ofn.lpstrFile,sizeof(TCHAR) * fileDlg.m_ofn.nMaxFile);
 
    //显示文件对话框,获得文件名集合
    if(fileDlg.DoModal() == IDOK){
         
        //获取第一个文件的位置
        POSITION pos_file;
        pos_file = fileDlg.GetStartPosition();
         
        //用CString数组存放文件的路径
        CArray<CString, CString> ary_filename;
        //存放文件的标题
        CArray<CString, CString> ary_fileTitle;
 
        //循环读出每个路径并存放在数组中
        while(pos_file != NULL){
             
            //将文件路径存放在数组中
            pathName = fileDlg.GetNextPathName(pos_file);
            ary_filename.Add(pathName);
             
            //获取文件名 
            //从字符串的后面往前遍历,如果遇到'\'则结束遍历,'\'右边的字符串则为文件名
            int length = pathName.GetLength();      
            for(int i = length -1; i>0;i--)
            {
                if('\' == pathName. GetAt(i))
                {//判断当前字符是否是'\'
                    fileName = pathName.Right(length - i -1);
                    break;//跳出循环
                }
            }//endfor
 
            //获取文件名(不包含后缀)
            //采用CString的Left(int count)截取CString中从左往右数的count个字符
            //fileName.GetLength()-4中的4表示".dat"四个字符
 
            fileTitle = fileName.Left(fileName.GetLength()-4);
            //AfxMessageBox(fileTitle);
            ary_fileTitle.Add(fileTitle);//将文件名(不包含后缀)添加到数组中
        }       
    }
    delete[] ch;
}

微信小程序扫码登陆

文章评论

1707人参与,0条评论