1. 选中一个文件夹
程序源码:
/*关键头文件*/
#include <shlobj.h>
#include <atlstr.h>
string SelectSingleDirectory()
{
CString strPath(_T(""));
BROWSEINFO bi;
bi.hwndOwner = NULL;
bi.pidlRoot = CSIDL_DESKTOP; //文件夹的根目录,此处为桌面
bi.pszDisplayName = NULL;
bi.lpszTitle = _T("请选择一个文件夹"); //显示位于对话框左上部的提示信息
bi.ulFlags = BIF_DONTGOBELOWDOMAIN | BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE; //有新建文件夹按钮
bi.lpfn = NULL;
bi.iImage = 0;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi); //调用选择对话框
if (pidl == NULL)
{
std::cout << "You havn't chosen any file!" << std::endl;
return string(CW2A(strPath.GetBuffer()));
}
TCHAR strFolder[1024];
SHGetPathFromIDList(pidl, strFolder);
strPath = strFolder;
return string(CW2A(strPath.GetBuffer()));
}
使用说明:
结果截图:
2.选中一个文件
程序源码:
/*关键头文件*/
#include <atlstr.h>
std::string SelectSingleFile()
{
std::string res = "";
TCHAR tmp[MAX_PATH] = { 0 };
GetCurrentDirectory(MAX_PATH, tmp); //取出当前的工作目录,保存到临时变量tmp中
TCHAR szBuffer[MAX_PATH] = { 0 };
OPENFILENAME ofn = { 0 };
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = _T(" *.flt\0*.flt\0 *.osg\0*.osg\0 *.ive\0*.ive\0 All Files(*.*)\0*.*\0"); //要选择的文件后缀
ofn.lpstrInitialDir = _T("F:\\Program\\Creator_flt\\"); //默认的文件路径
ofn.lpstrFile = szBuffer; //存放文件的缓冲区
ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer);
ofn.nFilterIndex = 0;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER; //标志如果是多选要加上OFN_ALLOWMULTISELECT
if (GetOpenFileName(&ofn) == 1)
{
res = CW2A(ofn.lpstrFile); //将宽字符集(Unicode)转化为多字符集(ASCII)
SetCurrentDirectory(tmp); //将tmp中的工作目录恢复,不恢复的后果就是程序运行的默认workspace将变成你刚选择的文件所在的目录下!!
}
else
{
std::cout << "You havn't chosen any file!" << std::endl;
}
return res;
}
使用说明:
运行截图:
3.选中多个文件
程序源码:
/*关键头文件*/
#include <atlstr.h>
vector<string> SelectMultiFiles()
{
TCHAR* iter = NULL;
OPENFILENAME ofn = { 0 };
TCHAR szDirectory[MAX_PATH] = {0}; //存放解析出的目录
TCHAR szBuffer[80 * MAX_PATH] = {0}; //在此设置最大支持的文件数
vector<string> res; //存放结果
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = _T("All Files(*.*)\0*.*\0"); //要选择的文件后缀
ofn.lpstrInitialDir = _T("F:\\Program\\Creator_flt\\"); //默认的文件路径
ofn.lpstrFile = szBuffer; //存放文件的缓冲区
ofn.nMaxFile = sizeof(szBuffer) / sizeof(*szBuffer);
ofn.nFilterIndex = 0;
ofn.Flags = OFN_EXPLORER | OFN_ALLOWMULTISELECT;
if (GetOpenFileName(&ofn) == 1)
{
lstrcpyn(szDirectory, szBuffer, ofn.nFileOffset);
lstrcat(szDirectory, _T("\\"));
iter = szBuffer + ofn.nFileOffset; //把指针指向第一个文件
while(*iter)
{
CString path = szDirectory;
path += iter;
string tmpPath = CW2A(path.GetBuffer());
res.push_back(tmpPath);
iter += (lstrlen(iter) + 1); //移至下一个文件
}
}
else
{
std::cout << "You havn't chosen any file!" << std::endl;
}
return res;
}
使用说明:
运行截图:
4.查找文件夹下指定格式的文件
程序源码:
/*关键头文件*/
#include <io.h>
vector<string> findFilesFrom(string directory)
{
directory += '\\';
string fileName;
vector<string> res;
struct _finddata_t fileInfo;
string targetFormat = directory + "*.cpp"; //目标格式
long findHandle = _findfirst(targetFormat.c_str(), &fileInfo);
if (findHandle == -1)
{
cout << "There dont have any target format files existed!" << endl;
_findclose(findHandle);
return res;
}
do
{
fileName = directory + fileInfo.name;
if (fileInfo.attrib == _A_ARCH)
{
res.push_back(fileName);
}
} while (_findnext(findHandle, &fileInfo) == 0);
_findclose(findHandle);
return res;
}