遍历卷并获得属性
1.GetLogicalDrivers或GetLogicalDriverStrings
获取主机上的逻辑驱动器
2.FindFirstVolume和FindNextVolume组合.
返回设备名:"}"形式的驱动器设备名
GetDriveType 获取驱动器类型
GetVolumeInformation 获取逻辑驱动器信息
GetLogicalDrivers
获取主机中所有的逻辑驱动器,以BitMap的形式返回
DWORD GetLogicalDrivers(void);
返回一个DWORD类型的值,第一位表示所对应的驱动器是否存在,一般情况下DWORD长度是32位
,在这个DWORD中,每一位对应了一个逻辑驱动器是否存在.
如第二位是"1"表示为"B",第四位为"1"表示"D"存在
GetLogicalDriverStrings获取主机中所有逻辑驱动器,以驱动器根路径字符返回
DWORD GetLogicalDriveString(
DWORD nBufferLength,
LPTSTR lpBuffer
);
nBufferLength:指数lpBuffer所指向的内存空间大小,以字节为单位
lpBuffer:指向存储返回结果字符串的内存空间
FindFirstVolume
查找主机中的第一个驱动器,返回驱动器设备名
HANDLE FindFirstVolum(
LPTSTR lpszVolumeName,
DWORD cchBufferLength
)
lpszVolumeName:指向驱动器名的内存缓冲区
cchBufferLength:参数lpszVolumeName所指向缓冲区的大小,以字节为单位
FindNextVolume
查找主机中后继的逻辑驱动器
BOOL FindNextVolume(
HANDLE hFindVolume,
LPTSTR lpszVolumeName,
DWORD cchBufferLength
)
hFindVolume:FindFirstVolume所返回驱动器查找句柄
lpszVolumeName:指向驱动器名的内存缓冲区
cchBufferLength:参数lpszVolumeName所指向缓冲区的大小,以字节为单
FindVolumeClose
关闭FindFirstVolume打开的卷遍历句柄
BOOL WINAPI FindVolumeClose(
HANDLE hFindVolume
);
GetDriverType
获取驱动器类型
GetDriverType(
LPCTSTR lpRootPathName
);
lpRootPathName:驱动器根路径,如"C:\"
GetVolumeInformation
获取逻辑驱动器信息
BOOL GetVolumeInformation(
LPCTSTR lpRootPathName,
LPTSTR lpVolumeNameBuffer,
DWORD nVolumeNameSize,
LPDWORD lpVolumeSerialNumber,
LPDWORD lpMaximumComponentLength,
LPDWORD lpFileSystemFlags,
LPTSTR lpFileSystemNameBuffer,
DWORD nFileSystemNameSize
);
注意:lpVolumeSerialNumber得到的只是卷区序列号,不是是硬盘Serial Number
会随格式化改变
lpRootPathName:输入参数,指向所要获取属性驱动器的根路径字符
lpVolumeNameBuffer:输出参数,返回驱动器名
nVolumeNameSize:输入参数,lpVolumeNameBuffer的内存缓冲区大小
lpVolumeSerialNumber:卷区序列号
lpMaximumComponentLength:输出参数,返回文件系统所支持的文件所组成部分的最大值
lpFileSystemFlags:输出参数,属性可以用来判断多种驱动器属性值,如
FILE_VOLUME_QUOTAS表示支持磁盘配额,FILE_SUPPORTS_ENCRYPTION表示文件系统是否支持EFS加密等.
nFileSystemNameSize:lpFileSystemNameBuffer的缓冲区大小
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
/* 预定义 */
#define BUFSIZE 1024
/* 函数申明 */
BOOL GetDirverInfo(LPSTR szDrive);
/* ************************************
* 功能 应用程序主函数,遍历驱动器并调用
* GetDirverInfo 获取驱动器属性
**************************************/
void main(void)
{
CHAR szLogicalDriveStrings[BUFSIZE];
PCHAR szDrive;
ZeroMemory(szLogicalDriveStrings,BUFSIZE);
// 获取逻辑驱动器卷标名
GetLogicalDriveStrings(BUFSIZE - 1,szLogicalDriveStrings);
szDrive = (PCHAR)szLogicalDriveStrings;
// 循环处理每个卷
do
{
if(!GetDirverInfo(szDrive))
{
printf("\nGet Volume Information Error: %d", GetLastError());
}
szDrive += (lstrlen(szDrive)+1);
}
while(*szDrive!='\x00');
}
/* ************************************
* BOOL GetDirverInfo(LPSTR szDrive)
* 功能 获取驱动器的属性
* 参数 LPSTR szDrive
* 指明要获取属性的驱动器的根路径 如 C:\
* 返回值 BOOL 是否成功
**************************************/
BOOL GetDirverInfo(LPSTR szDrive)
{
UINT uDriveType;
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD dwFileSystemFlags;
TCHAR szFileSystemNameBuffer[BUFSIZE];
printf("\n%s\n",szDrive);
uDriveType = GetDriveType(szDrive);
// 判断类型
switch(uDriveType)
{
case DRIVE_UNKNOWN:
printf("The drive type cannot be determined. ");
break;
case DRIVE_NO_ROOT_DIR:
printf("The root path is invalid, for example, no volume is mounted at the path. ");
break;
case DRIVE_REMOVABLE:
printf("The drive is a type that has removable media, for example, a floppy drive or removable hard disk. ");
break;
case DRIVE_FIXED:
printf("The drive is a type that cannot be removed, for example, a fixed hard drive. ");
break;
case DRIVE_REMOTE:
printf("The drive is a remote (network) drive. ");
break;
case DRIVE_CDROM:
printf("The drive is a CD-ROM drive. ");
break;
case DRIVE_RAMDISK:
printf("The drive is a RAM disk. ");
break;
default:
break;
}
if (!GetVolumeInformation(
szDrive, NULL, 0,
&dwVolumeSerialNumber,
&dwMaximumComponentLength,
&dwFileSystemFlags,
szFileSystemNameBuffer,
BUFSIZE
))
{
return FALSE;
}
printf ("\nVolume Serial Number is %u",dwVolumeSerialNumber);
printf ("\nMaximum Component Length is %u",dwMaximumComponentLength);
printf ("\nSystem Type is %s\n",szFileSystemNameBuffer);
if(dwFileSystemFlags & FILE_SUPPORTS_REPARSE_POINTS)
{
printf ("The file system does not support volume mount points.\n");
}
if(dwFileSystemFlags & FILE_VOLUME_QUOTAS)
{
printf ("The file system supports disk quotas.\n");
}
if(dwFileSystemFlags & FILE_CASE_SENSITIVE_SEARCH)
{
printf ("The file system supports case-sensitive file names.\n");
}
//you can use these value to get more informaion
//
//FILE_CASE_PRESERVED_NAMES
//FILE_CASE_SENSITIVE_SEARCH
//FILE_FILE_COMPRESSION
//FILE_NAMED_STREAMS
//FILE_PERSISTENT_ACLS
//FILE_READ_ONLY_VOLUME
//FILE_SUPPORTS_ENCRYPTION
//FILE_SUPPORTS_OBJECT_IDS
//FILE_SUPPORTS_REPARSE_POINTS
//FILE_SUPPORTS_SPARSE_FILES
//FILE_UNICODE_ON_DISK
//FILE_VOLUME_IS_COMPRESSED
//FILE_VOLUME_QUOTAS
printf("...\n");
return TRUE;
}
使用FindFirstVolume,FindNextVolume和FindVolumeClose函数,实现遍历驱动器,获取驱动名称功能
#define _WIN32_WINNT 0x0501
/* 头文件 */
#include <windows.h>
#include <stdio.h>
/* 预定义 */
#define BUFSIZE MAX_PATH
/* 函数申明 */
BOOL GetDirverInfo(LPSTR szDrive);
/* ************************************
* 功能 应用程序主函数,遍历驱动器并调用
* GetDirverInfo 获取驱动器属性
**************************************/
int main(void)
{
TCHAR buf[BUFSIZE]; // 卷标信息
HANDLE hVol; // 卷遍历句柄
BOOL bFlag;
hVol = FindFirstVolume (buf, BUFSIZE );
if (hVol == INVALID_HANDLE_VALUE)
{
printf (TEXT("No volumes found!\n"));
return (-1);
}
GetDirverInfo (buf);
while( FindNextVolume(
hVol,
buf,
BUFSIZE
))
{
GetDirverInfo (buf);
}
bFlag = FindVolumeClose(
hVol
);
return (bFlag);
}
/* ************************************
* BOOL GetDirverInfo(LPSTR szDrive)
* 功能 获取驱动器的属性
* 参数 LPSTR szDrive
* 指明要获取属性的驱动器的根路径 如 C:\
* 返回值 BOOL 是否成功
**************************************/
BOOL GetDirverInfo(LPSTR szDrive)
{
UINT uDriveType;
DWORD dwVolumeSerialNumber;
DWORD dwMaximumComponentLength;
DWORD dwFileSystemFlags;
CHAR szFileSystemNameBuffer[BUFSIZE];
CHAR szDirveName[MAX_PATH];
printf("\n%s\n",szDrive);
uDriveType = GetDriveType(szDrive);
switch(uDriveType)
{
case DRIVE_UNKNOWN:
printf("The drive type cannot be determined. ");
break;
case DRIVE_NO_ROOT_DIR:
printf("The root path is invalid, for example, no volume is mounted at the path. ");
break;
case DRIVE_REMOVABLE:
printf("The drive is a type that has removable media, for example, a floppy drive or removable hard disk. ");
break;
case DRIVE_FIXED:
printf("The drive is a type that cannot be removed, for example, a fixed hard drive. ");
break;
case DRIVE_REMOTE:
printf("The drive is a remote (network) drive. ");
break;
case DRIVE_CDROM:
printf("The drive is a CD-ROM drive. ");
break;
case DRIVE_RAMDISK:
printf("The drive is a RAM disk. ");
break;
default:
break;
}
if (!GetVolumeInformation(
szDrive,
szDirveName,
MAX_PATH,
&dwVolumeSerialNumber,
&dwMaximumComponentLength,
&dwFileSystemFlags,
szFileSystemNameBuffer,
BUFSIZE
))
{
return FALSE;
}
if(0!=lstrlen(szDirveName))
{
printf ("\nDrive Name is %s\n",szDirveName);
}
printf ("\nVolume Serial Number is %u",dwVolumeSerialNumber);
printf ("\nMaximum Component Length is %u",dwMaximumComponentLength);
printf ("\nSystem Type is %s\n",szFileSystemNameBuffer);
if(dwFileSystemFlags & FILE_SUPPORTS_REPARSE_POINTS)
{
printf ("The file system does not support volume mount points.\n");
}
if(dwFileSystemFlags & FILE_VOLUME_QUOTAS)
{
printf ("The file system supports disk quotas.\n");
}
if(dwFileSystemFlags & FILE_CASE_SENSITIVE_SEARCH)
{
printf ("The file system supports case-sensitive file names.\n");
}
//you can use these value to get more informaion
//
//FILE_CASE_PRESERVED_NAMES
//FILE_CASE_SENSITIVE_SEARCH
//FILE_FILE_COMPRESSION
//FILE_NAMED_STREAMS
//FILE_PERSISTENT_ACLS
//FILE_READ_ONLY_VOLUME
//FILE_SUPPORTS_ENCRYPTION
//FILE_SUPPORTS_OBJECT_IDS
//FILE_SUPPORTS_REPARSE_POINTS
//FILE_SUPPORTS_SPARSE_FILES
//FILE_UNICODE_ON_DISK
//FILE_VOLUME_IS_COMPRESSED
//FILE_VOLUME_QUOTAS
printf("...\n");
return TRUE;
}