首先还是贴代码
VC++ Code:
/* *************************************《精通Windows API》 * 示例代码* GetVolumeInfo.c* 4.2.1 遍历驱动器并获取驱动器属性**************************************//* 头文件 */ #include <windows.h> #include <stdlib.h> #include <stdio.h> #include<string.h> /* 预定义 */ #define BUFSIZE 1024 /* 函数申明 */ BOOL GetDirverInfo(LPSTR szDrive);/* ************************************* 功能 应用程序主函数,遍历驱动器并调用* GetDirverInfo 获取驱动器属性**************************************/ void main(void) {CHAR szLogicalDriveStrings[BUFSIZE];PCHAR szDrive;CHAR lStr;int i ;ZeroMemory(szLogicalDriveStrings,BUFSIZE);// 获取逻辑驱动器卷标名GetLogicalDriveStrings(BUFSIZE - 1,szLogicalDriveStrings); //strcpy(lStr,szLogicalDriveStrings,sizeof(szLogicalDriveStrings)/sizeof(CHAR));szDrive = (PCHAR)szLogicalDriveStrings;// 循环处理每个卷do{if(!GetDirverInfo(szDrive)){printf("\nGet Volume Information Error: %d", GetLastError());}szDrive += (lstrlen(szDrive)+1);//szDrive = szDrive +lstrlen(szDrive); }while(*szDrive!='\x00');scanf("%d",&i); }/* ************************************* 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_QUOTASprintf("...\n");return TRUE; }
编译后运行:
vb6 Code(转自网上):
1).类模块中:
Option Explicit'============= 类模块里,名称:GetDiskDrive =========== 'local variable(s) to hold property value(s) Private mvarDriveCount As Integer 'local copy'Value Name Meaning '------------------------------------------------------------ '0 DRIVE_UNKNOWN The drive type cannot be determined. '1 DRIVE_NO_ROOT_DIR The root directory does not exist. '2 DRIVE_REMOVABLE The disk can be removed from the drive. '3 DRIVE_FIXED The disk cannot be removed from the drive. '4 DRIVE_REMOTE The drive is a remote (network) drive. '5 DRIVE_CDROM The drive is a CD-ROM drive. '6 DRIVE_RAMDISK The drive is a RAM disk. '------------------------------------------------------------- Public Enum DriveTypeDRIVE_UNKNOWN = 0DRIVE_NO_ROOT_DIR = 1DRIVE_REMOVABLE = 2DRIVE_FIXED = 3DRIVE_REMOTE = 4DRIVE_CDROM = 5DRIVE_RAMDISK = 6 End EnumPrivate drvType(26) As Integer Private drvName(26) As String 'Attribute drvName.VB_VarDescription = "DriveName"Private Declare Function GetLogicalDriveStrings Lib "kernel32" Alias "GetLogicalDriveStringsA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long Private Declare Function GetDriveTypeA Lib "kernel32" (ByVal nDrive As String) As LongPublic Function GetDriveType(ByVal n As Integer) As Integer 'Attribute GetDriveType.VB_Description = "获得驱动器类型"GetDriveType = drvType(n) End FunctionPublic Function GetDriveName(ByVal n As Integer) As String 'Attribute GetDriveName.VB_Description = "获得驱动器名称"GetDriveName = drvName(n) End FunctionPublic Function GetDriveTypeName(ByVal n As Integer) As StringDim sTypeName As StringSelect Case nCase DRIVE_NO_ROOT_DIRsTypeName = "根目录不存在"Case DRIVE_REMOVABLEsTypeName = "可移动磁盘,如软盘"Case DRIVE_FIXEDsTypeName = "磁盘"Case DRIVE_REMOTEsTypeName = "磁盘映射"Case DRIVE_CDROMsTypeName = "CD-ROM"Case DRIVE_RAMDISKsTypeName = "U盘"Case ElsesTypeName = "未知类型"End SelectGetDriveTypeName = sTypeName End FunctionPublic Property Let DriveCount(ByVal vData As Integer) 'Attribute DriveCount.VB_Description = "驱动器个数" 'used when assigning a value to the property, on the left side of an assignment. 'Syntax: X.DriveCount = 5mvarDriveCount = vData End PropertyPublic Property Get DriveCount() As Integer 'used when retrieving value of a property, on the right side of an assignment. 'Syntax: Debug.Print X.DriveCountDriveCount = mvarDriveCount End Property'With this class you can get the systme disk Drive Name and its type Private Sub Class_Initialize()Dim PathStr As String * 200Dim DriveStr As StringDim L, i As IntegermvarDriveCount = 0If GetLogicalDriveStrings(200, PathStr) <> 0 ThenDriveStr = Mid(PathStr, 1, InStr(1, PathStr, Chr$(0) & Chr$(0)))L = Len(DriveStr)For i = 1 To L Step 4mvarDriveCount = mvarDriveCount + 1drvName(mvarDriveCount) = Mid(DriveStr, i, 3)drvType(mvarDriveCount) = GetDriveTypeA(drvName(mvarDriveCount))Next iEnd If End Sub
2):窗体中:
Private Sub Command4_Click()Dim i As IntegerDim drv As clsFileSystemAPIDim sResult As StringSet drv = New clsFileSystemAPIsResult = "驱动器总数为:" + CStr(drv.DriveCount) + vbCrLfFor i = 1 To drv.DriveCountsResult = sResult + drv.GetDriveName(i) + vbTab + drv.GetDriveTypeName(drv.GetDriveType(i)) + vbCrLfNext iLabel1.Caption = sResult End Sub
运行: