枚举当前进程目录下的文件【改进版】
昨天那段代码,写得实在是烂啊,今天做了一下改进,麻烦管理员把原帖删了。原帖地址:http://bbs.fishc.com/thread-60923-1-1.html (出处: 鱼C论坛)改进代码如下:
vector<wstring> EnumFilesEx()
{
vector<wstring> vectFilePath;
WCHAR szBaseFilePath = {0}; //保存文件路径
WCHAR szEnumFilePath = {0}; //保存文件路径和格式
WCHAR szAddFilePath = {0};
HANDLE hFile = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA mFileData = {0};
HANDLE hFileRead = 0;
// 获取当前进程路径
if (0 == GetModuleFileName(NULL, szBaseFilePath, sizeof(szBaseFilePath)))
{
vectFilePath.clear();
return std::move(vectFilePath);
}
PathRemoveFileSpec(szBaseFilePath);
PathAppend(szBaseFilePath, L"\\resource");
RtlCopyMemory(szEnumFilePath, szBaseFilePath, sizeof(szBaseFilePath));
PathAppend(szEnumFilePath, L"\\*.txt");
// 查找文件下的第一个文件
hFile = FindFirstFile(szEnumFilePath, &mFileData);
if (INVALID_HANDLE_VALUE == hFile)
{
vectFilePath.clear();
return std::move(vectFilePath);
}
RtlCopyMemory(szAddFilePath, szBaseFilePath, sizeof(szBaseFilePath));
PathAppend(szAddFilePath, mFileData.cFileName);
vectFilePath.push_back(szAddFilePath); // 将文件名保存在vector数组中
// 循环枚举文件名称
for (DWORD i = 0; FALSE != FindNextFile(hFile, &mFileData); i++ )
{
RtlZeroMemory(szAddFilePath, sizeof(szAddFilePath));
RtlCopyMemory(szAddFilePath, szBaseFilePath, sizeof(szBaseFilePath));
PathAppend(szAddFilePath, mFileData.cFileName);
vectFilePath.push_back(szAddFilePath);
}
CloseHandle(hFileRead);
return std::move(vectFilePath);
}
页:
[1]