在VC++编程中,获取文件的路径很容易通过CFileDialog来实现,代码示例如下:
CFileDialog dlg(
TRUE,"*.csv",NULL,
OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,
"csv files(*.csv)|*.csv|All Files(*.*)|*.*||",NULL
);
dlg.m_ofn.lpstrTitle = "Open Arousal File"; //the title of the openfile dailog
if(dlg.DoModal()!= IDOK) //if don't get the arousal file
return;
arousalPath = dlg.GetPathName(); //get the arousal address
UpdateData(FALSE);
通过上面的代码就可以获得一个文件的绝对路径,但有时我们需要获得文件夹的路径以便后续的操作,相对来说,获取文件夹的路径要复杂一点。代码如下:
TCHAR szPath[_MAX_PATH];
BROWSEINFO bi;
bi.hwndOwner = GetSafeHwnd();
bi.pidlRoot = NULL;
bi.lpszTitle = "Please select the input path";
bi.pszDisplayName = szPath;
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = NULL;
LPITEMIDLIST pItemIDList = SHBrowseForFolder(&bi);
if(pItemIDList)
{
if(SHGetPathFromIDList(pItemIDList,szPath))
{
InputPath = szPath;
//videoPath = inputImgPath;
}
//use IMalloc interface for avoiding memory leak
IMalloc* pMalloc;
if( SHGetMalloc(&pMalloc) != NOERROR )
{
TRACE(_T("Can't get the IMalloc interface\n"));
}
pMalloc->Free(pItemIDList);
if(pMalloc)
pMalloc->Release();
UpdateData(FALSE);
}
else
InputPath = "";