1、卷->卷标
使用FindFirstVolume()和FindNextVolume()函数体系,枚举系统所有卷(Volume)的例子,然后获取卷标、卷类型。这个方式可以枚举出没有驱动器号(卷标)的卷。
int TestMode1()
{HANDLE hVolume = INVALID_HANDLE_VALUE;TCHAR tszVolumeName[MAX_PATH] = {0};TCHAR tszVolumePath[MAX_PATH] = {0};BOOL bRet;DWORD dwError;DWORD dwCount;UINT nDriveType;//1、获取卷名称hVolume = FindFirstVolume(tszVolumeName, ARRAYSIZE(tszVolumeName));if (hVolume == INVALID_HANDLE_VALUE){wprintf(_T("FindFirstVolume() failed, error code = %d"), GetLastError());return -1;}while (1){//2、获取卷路径dwCount = ARRAYSIZE(tszVolumePath);bRet = GetVolumePathNamesForVolumeName(tszVolumeName, tszVolumePath, dwCount, &dwCount);if (!bRet){dwError = GetLastError();wprintf(_T("GetVolumePathNamesForVolumeName() failed, error code = %d"), dwError);getchar();return -1;}//3、获取驱动器类型nDriveType = GetDriveType(tszVolumePath);wprintf(_T("Volume name: %s\r\n"), tszVolumeName);wprintf(_T("Volume path: %s\r\n"), tszVolumePath);wprintf(_T("Drive type: %d\r\n\r\n"), nDriveType);bRet = FindNextVolume(hVolume, tszVolumeName, ARRAYSIZE(tszVolumeName));if (!bRet){dwError = GetLastError();if (dwError != ERROR_NO_MORE_FILES){wprintf(_T("FindNextVolume() failed, error code = %d"), dwError);return -1;}break;}}wprintf(_T("Test 2 end!\r\n\r\n"));//getchar();return 0;
}
输入
2、卷标->卷名
通过卷标(驱动器号)获取卷名,需要使用GetVolumeNameForVolumeMountPoint()函数。
int TestMode2()
{TCHAR tszLogicDriveStrings[1024] = {0};LPCTSTR szDrive = NULL;TCHAR tszVolumeName[MAX_PATH];GetLogicalDriveStrings(sizeof(tszLogicDriveStrings) / sizeof(TCHAR) - 1, tszLogicDriveStrings);szDrive = (LPCTSTR)tszLogicDriveStrings;do{GetVolumeNameForVolumeMountPoint(szDrive, tszVolumeName, sizeof(tszVolumeName) / sizeof(TCHAR));wprintf(_T("Drive %s, Volume name: %s\r\n"), szDrive, tszVolumeName);szDrive += (lstrlen(szDrive) + 1);} while (*szDrive != _T('\x00'));wprintf(_T("\r\nTest 1 end!\r\n\r\n"));return 0;
}
输出