怎样用API得到硬盘的大小

解决方案 »

  1.   

    Dim fso As New FileSystemObject
         Dim drv As Drive
         Dim s As String
           Set drv = fso.GetDrive(fso.GetDriveName("D:"))
         s = "Drive" & UCase("D:") & "     " & drv.VolumeName & vbCrLf
             s = s & "       " & "Total Space  : " & FormatNumber(drv.TotalSize / 1024 / 1024 / 1024) & "GB     "
         s = s & "Free Space:  " & FormatNumber(drv.FreeSpace / 1024 / 1024 / 1024) & "Gb" & vbCrLf
        s 的值就是D:的大小
      

  2.   

    你完全可以用上面的办法来把所有的分区加起来啊,你也可以用 GetdriverType GetDiskFreespace 等来实现。
      

  3.   

    'In general section
    Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTtoalNumberOfClusters As Long) As LongPrivate Sub Form_Load()
        Dim Sectors as Long,Bytes as Long,FreeC as Long, TotalC as Long,Total as Long,Freeb as Long
        'Retrieve information about the C:\
        GetDiskFreeSpace "C:\", Sectors, Bytes, Freec, Totalc
        'Set graphic mode to persistent
        Me.AutoRedraw = True
        'Print the information to the form
        Me.Print " Path: C:\"
        Me.Print " Sectors per Cluster:" + Str$(Sector)
        Me.Print " Bytes per sector:" + Str$(Bytes)
        Me.Print " Number Of Free Clusters:" + Str$(Freec)
        Me.Print " Total Number Of Clusters:" + Str$(Totalc)
        Total = rTotalc& * rSector& * rBytes&
        Me.Print " Total number of bytes in path:" + Str$(Total)
        Freeb = rFreec& * rSector& * rBytes&
        Me.Print " Free bytes:" + Str$(Freeb)
    End sub
      

  4.   

    在不知道硬盘有多少分区的情况下怎么通过API知道硬盘的大小
      

  5.   

    Option ExplicitPrivate Enum MEDIA_TYPE
        Unknown                '// Format is unknown
        F5_1Pt2_512 '           // 5.25", 1.2MB,  512 bytes/sector
        F3_1Pt44_512 '          // 3.5",  1.44MB, 512 bytes/sector
        F3_2Pt88_512 '           // 3.5",  2.88MB, 512 bytes/sector
        F3_20Pt8_512 '           // 3.5",  20.8MB, 512 bytes/sector
        F3_720_512 '             // 3.5",  720KB,  512 bytes/sector
        F5_360_512 '             // 5.25", 360KB,  512 bytes/sector
        F5_320_512 '             // 5.25", 320KB,  512 bytes/sector
        F5_320_1024 '            // 5.25", 320KB,  1024 bytes/sector
        F5_180_512 '             // 5.25", 180KB,  512 bytes/sector
        F5_160_512 '             // 5.25", 160KB,  512 bytes/sector
        RemovableMedia '         // Removable media other than floppy
        FixedMedia '             // Fixed hard disk media
        F3_120M_512 '            // 3.5", 120M Floppy
        F3_640_512 '             // 3.5" ,  640KB,  512 bytes/sector
        F5_640_512 '             // 5.25",  640KB,  512 bytes/sector
        F5_720_512 '             // 5.25",  720KB,  512 bytes/sector
        F3_1Pt2_512 '            // 3.5" ,  1.2Mb,  512 bytes/sector
        F3_1Pt23_1024 '          // 3.5" ,  1.23Mb, 1024 bytes/sector
        F5_1Pt23_1024 '          // 5.25",  1.23MB, 1024 bytes/sector
        F3_128Mb_512 '           // 3.5" MO 128Mb   512 bytes/sector
        F3_230Mb_512 '           // 3.5" MO 230Mb   512 bytes/sector
        F8_256_128 '              // 8",     256KB,  128 bytes/sector
    End Enum
    Private Type DISK_GEOMETRY
        Cylinders As Long
        Cylinders1 As Long
        MediaType As MEDIA_TYPE
        TracksPerCylinder As Long
        SectorsPerTrack As Long
        BytesPerSector As Long
    End TypePrivate Const GENERIC_READ = &H80000000
    Private Const OPEN_EXISTING = 3
    Private Const FILE_SHARE_READ = &H1
    Private Const IOCTL_DISK_GET_DRIVE_GEOMETRY = &H70000Private Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, _
                                                        ByVal dwDesiredAccess As Long, _
                                                        ByVal dwShareMode As Long, _
                                                        lpSecurityAttributes As Long, _
                                                        ByVal dwCreationDisposition As Long, _
                                                        ByVal dwFlagsAndAttributes As Long, _
                                                        ByVal hTemplateFile As Long) As Long
    Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
    Private Declare Function DeviceIoControl Lib "kernel32" (ByVal hDevice As Long, _
                                                ByVal dwIoControlCode As Long, _
                                                lpInBuffer As Any, ByVal nInBufferSize As Long, _
                                                lpOutBuffer As Any, ByVal nOutBufferSize As Long, _
                                                lpBytesReturned As Long, lpOverlapped As Any) As Long
    Private Sub GetDriverSize()
        Dim diskh As Long
        Dim retbyte As Long
        Dim diskspace As Double
        Dim disknum As Integer
        Dim result As Long
        Dim disk_stru As DISK_GEOMETRY
        Dim p As String
       
        p = "\\.\PhysicalDrive0"
       
        diskh = CreateFile(p, GENERIC_READ, FILE_SHARE_READ, ByVal 0, OPEN_EXISTING, 0, 0)
     
        result = DeviceIoControl(diskh, IOCTL_DISK_GET_DRIVE_GEOMETRY, Null, 0, disk_stru, Len(disk_stru), retbyte, 0)
                                        
        diskspace = disk_stru.BytesPerSector * (disk_stru.SectorsPerTrack / 1024) * _
                    (disk_stru.TracksPerCylinder / 1024) * (disk_stru.Cylinders / 1024)
        
        CloseHandle diskh
        MsgBox "硬盘大小为: " & Format(diskspace, "00.00") & "GB"
    End Sub