• Windows API 逐个逐个学MessageBox(5) 遍历驱动器并获取驱动器属性 GetLogicalDriveStrings、GetDriveTypeA


          首先还是贴代码

          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_QUOTAS
        printf("...\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 DriveType
         DRIVE_UNKNOWN = 0
         DRIVE_NO_ROOT_DIR = 1
         DRIVE_REMOVABLE = 2
         DRIVE_FIXED = 3
         DRIVE_REMOTE = 4
         DRIVE_CDROM = 5
         DRIVE_RAMDISK = 6
    End Enum
    
    Private 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 Long
    
    Public Function GetDriveType(ByVal n As Integer) As Integer
    'Attribute GetDriveType.VB_Description = "获得驱动器类型"
         GetDriveType = drvType(n)
    End Function
    
    Public Function GetDriveName(ByVal n As Integer) As String
    'Attribute GetDriveName.VB_Description = "获得驱动器名称"
         GetDriveName = drvName(n)
    End Function
    
    Public Function GetDriveTypeName(ByVal n As Integer) As String
         Dim sTypeName As String
         
         Select Case n
             Case DRIVE_NO_ROOT_DIR
                 sTypeName = "根目录不存在"
             Case DRIVE_REMOVABLE
                 sTypeName = "可移动磁盘,如软盘"
             Case DRIVE_FIXED
                 sTypeName = "磁盘"
             Case DRIVE_REMOTE
                 sTypeName = "磁盘映射"
             Case DRIVE_CDROM
                 sTypeName = "CD-ROM"
             Case DRIVE_RAMDISK
                 sTypeName = "U盘"
             Case Else
                 sTypeName = "未知类型"
         End Select
        
         GetDriveTypeName = sTypeName
    End Function
    
    Public 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 = 5
         mvarDriveCount = vData
    End Property
    
    Public Property Get DriveCount() As Integer
    'used when retrieving value of a property, on the right side of an assignment.
    'Syntax: Debug.Print X.DriveCount
         DriveCount = 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 * 200
         Dim DriveStr As String
         Dim L, i As Integer
    
         mvarDriveCount = 0
        
         If GetLogicalDriveStrings(200, PathStr) <> 0 Then
             DriveStr = Mid(PathStr, 1, InStr(1, PathStr, Chr$(0) & Chr$(0)))
             L = Len(DriveStr)
             For i = 1 To L Step 4
                 mvarDriveCount = mvarDriveCount + 1
                 drvName(mvarDriveCount) = Mid(DriveStr, i, 3)
                 drvType(mvarDriveCount) = GetDriveTypeA(drvName(mvarDriveCount))
             Next i
         End If
    End Sub

           2):窗体中:

    Private Sub Command4_Click()
         Dim i As Integer
         Dim drv As clsFileSystemAPI
         Dim sResult As String
        
         Set drv = New clsFileSystemAPI
        
         sResult = "驱动器总数为:" + CStr(drv.DriveCount) + vbCrLf
         For i = 1 To drv.DriveCount
             sResult = sResult + drv.GetDriveName(i) + vbTab + drv.GetDriveTypeName(drv.GetDriveType(i)) + vbCrLf
         Next i
         Label1.Caption = sResult
    End Sub

            运行:

     

      

  • 相关阅读:
    Google哲学(一)
    Predictably Irractional 相对论的真相
    .NET使用OpenSSL生成的pem密钥文件【做电子商务的朋友可能需要】
    从开辟蓝海到保卫蓝海(一)
    礼让?
    登门槛策略
    从开辟蓝海到保卫蓝海(四)
    盛大招聘 高级数据库开发工程师 工作地点张江高科 学历高者,经验可放宽
    Show一下拿的奖杯
    我们家的一坨和田仔玉[三色皮]
  • 原文地址:https://www.cnblogs.com/xiaoguanqiu/p/2933451.html
Copyright © 2020-2023  润新知