rt

解决方案 »

  1.   

    Option Explicit'   API声明
    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()
        GetDriveInfo
    End Sub'   选中一个驱动器时
    Private Sub drvSelect_Change()
        GetDriveInfo
    End Sub'   获取磁盘剩余空间信息并显示在相应的标签中
    Private Function GetDriveInfo()
    Dim sDrive As String
    Dim lBytes As Long
    Dim lMBFree As Double
    Dim lMBTotal As Double
    Dim lSecPerClust As Long    '   扇区每簇
    Dim lBytePerSect As Long    '   字节每扇区
    Dim lNumFreeClust As Long   '   空白簇的数目
    Dim lTotalNumClust As Long  '   簇的总数
        
        '   获取盘符
        sDrive = Left$(DrvSelect.Drive, 1)
        sDrive = sDrive & ":\"
        '   调用API函数返回剩余空间
        lBytes = GetDiskFreeSpace(sDrive, lSecPerClust, lBytePerSect, lNumFreeClust, lTotalNumClust)
        '   转换以兆为单位
        lMBFree = ((lSecPerClust * lNumFreeClust) / 1024) * lBytePerSect / 1024
        lMBTotal = ((lSecPerClust * lTotalNumClust) / 1024) * lBytePerSect / 1024
        '   显示信息
        lblSecPerClust.Caption = lSecPerClust & " Sectors/Cluster"
        lblBytePerSect.Caption = lBytePerSect & " Bytes/Sector"
        lblNumFreeClust.Caption = lNumFreeClust & " Free Clusters"
        lblTotalNumClust.Caption = lTotalNumClust & " Total Clusters"
        lblTotalSpace.Caption = lMBTotal & " MB Total Space"
        lblSpaceFree.Caption = lMBFree & " MB Free on Drive " & DrvSelect.Drive
    End Function
      

  2.   

    先用 GetDrive 得到驱动器
    然后用 GetDiskFreeSpace 得到可用空间
      

  3.   

    在程序中放2个label控件和1个textbox控件
    Text1.text:盘符
    labelT:总容量
    labelF:自由空间Private Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String, lpSectorsPerCluster As Long, lpBytesPerSector As Long, lpNumberOfFreeClusters As Long, lpTotalNumberOfClusters As Long) As LongPrivate Sub Command1_Click()
        Dim a, b, c, d, free, total As Long
        Dim disk As String
        disk = Text1.Text
        GetDiskFreeSpace disk, a, b, c, d
        '计算磁盘总容量
        total = d * b * a / 1024 / 1024
        '计算磁盘自由空间
        free = c * a * b / 1024 / 1024
        '显示磁盘总容量与自由空间
        LabT.Caption = total & "MB"
        LabF.Caption = free & "MB"
    End Sub
      

  4.   

    我用了filesystemobject,谢谢几位。