根据文件句柄,获取文件名
232 views 一月 31, 05 by Timothy#include #define BUFSIZE 512 BOOL GetFileNameFromHandle(HANDLE hFile) // Get the file size. if( dwFileSizeLo == 0 && dwFileSizeHi == 0 ) // Create a file mapping object. if (hFileMap) if (pMem) // Translate path with device name to drive letters. if (GetLogicalDriveStrings(BUFSIZE-1, szTemp)) do // Look up each device name if (uNameLen < MAX_PATH) if (bFound) // Go to the next NULL character. CloseHandle(hFileMap);
#include
#include
#include
#include
{
BOOL bSuccess = FALSE;
TCHAR pszFilename[MAX_PATH+1];
HANDLE hFileMap;
DWORD dwFileSizeHi = 0;
DWORD dwFileSizeLo = GetFileSize(hFile, &dwFileSizeHi);
{
printf(“Cannot map a file with a length of zero.\n”);
return FALSE;
}
hFileMap = CreateFileMapping(hFile,
NULL,
PAGE_READONLY,
0,
1,
NULL);
{
// Create a file mapping to get the file name.
void* pMem = MapViewOfFile(hFileMap, FILE_MAP_READ, 0, 0, 1);
{
if (GetMappedFileName (GetCurrentProcess(),
pMem,
pszFilename,
MAX_PATH))
{
TCHAR szTemp[BUFSIZE];
szTemp[0] = ‘\0′;
{
TCHAR szName[MAX_PATH];
TCHAR szDrive[3] = TEXT(” :”);
BOOL bFound = FALSE;
TCHAR* p = szTemp;
{
// Copy the drive letter to the template string
*szDrive = *p;
if (QueryDosDevice(szDrive, szName, BUFSIZE))
{
UINT uNameLen = _tcslen(szName);
{
bFound = _tcsnicmp(pszFilename, szName,
uNameLen) == 0;
{
// Reconstruct pszFilename using szTemp
// Replace device path with DOS path
TCHAR szTempFile[MAX_PATH];
_stprintf(szTempFile,
TEXT(“%s%s”),
szDrive,
pszFilename+uNameLen);
_tcsncpy(pszFilename, szTempFile, MAX_PATH);
}
}
}
while (*p++);
} while (!bFound && *p); // end of string
}
}
bSuccess = TRUE;
UnmapViewOfFile(pMem);
}
}
printf(“File name is %s\n”, pszFilename);
return(bSuccess);
}
你好,想请教你一个问题,
我想从一个文件夹中的得到文件名的string该怎么办?
只知道文件夹的路径,不知道里面都有什么文件
可以枚举的啊。