以下是关于MSDN中的有关详细说明:对于大于的2G分区,GetDiskFreeSpace函数可能(什么可能,是一定!)返回错误的值,此时函数会屏蔽存在lpNumberOfFreeClusters及lpTotalNumberOfClusters中的值,因此建议不要用该函数来获得大于2G分区的信息。对于大于2G的分区应当使用GetDiskFreeSpaceEx函数(从Win95 OEM OSR2开始),此函数可以返回分区的有关正确信息。
找到了问题所在便可以对症下药了,即用GetDiskFreeSpaceEx函数代替GetDiskFreeSpace函数即可。以下是GetDiskFreeSpaceEx函数中所要传递增的参数
lpRootPathName String ,不包括卷名的磁盘根路径名
lpFreeBytesAvailableToCaller LARGE_INTEGER,指定一个变量,用于容纳调用者可用的字节数量
lpTotalNumberOfBytes LARGE_INTEGER ,指定一个变量,用于容纳磁盘上的总字节数
lpTotalNumberOfFreeBytes LARGE_INTEGER,指定一个变量,用于容纳磁盘上可用的字节数
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
我们可以看到LARGE_INTEGER是一个由两个long型组成的一个类型,两个long组成表示的都是无符号的数,在转换时应当定义一个single型的变量,使其等于highpart*(2^32-1) + lowpart,注意此处的两个long型相当于C/C++中的无符号型整数类型,因为在VB中不存在此种类型,故而在换算时要处理好转换关系。我本人的做法是首先判断long型变量的正负,如是正直接相乘,如是负则用2^32-1减去该值再相乘(具体算法详见下面的代码)。
Option Explicit
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
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 Long
Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" _
(ByVal lpRootPathName As String, lpFreeBytesAvailableToCaller As LARGE_INTEGER, _
lpTotalNumberOfBytes As LARGE_INTEGER, lpTotalNumberOfFreeBytes _
As LARGE_INTEGER) As Long
Private Sub Command1_Click()
Dim lngSectors&
Dim lngTotalCluster&
Dim lngFreeCluster&
Dim lngPerCluster&
Dim lngperBytes&
Dim lngSize#
GetDiskFreeSpace "c:\", lngPerCluster, lngperBytes, lngFreeCluster, lngTotalCluster
MsgBox CStr(lngTotalCluster * lngperBytes * lngPerCluster)
Debug.Print lngTotalCluster, lngperBytes, lngPerCluster
End Sub
Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type
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 Long
Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA" _
(ByVal lpRootPathName As String, lpFreeBytesAvailableToCaller As LARGE_INTEGER, _
lpTotalNumberOfBytes As LARGE_INTEGER, lpTotalNumberOfFreeBytes _
As LARGE_INTEGER) As Long
Private Sub Command1_Click()
注释:用GetDiskFreeSpace得到错误的容量
Dim lngSectors&
Dim lngTotalCluster&
Dim lngFreeCluster&
Dim lngPerCluster&
Dim lngperBytes&
Dim lngSize#
GetDiskFreeSpace "c:\", lngPerCluster, lngperBytes, lngFreeCluster, lngTotalCluster
MsgBox CStr(lngTotalCluster * lngperBytes * lngPerCluster)
End Sub
Private Sub cmdStart_Click()
注释:用GetDiskFreeSpaceEx得到正确的容量
Dim lngFreeCaller As LARGE_INTEGER
Dim lngTotal As LARGE_INTEGER
Dim lngTotalFree As LARGE_INTEGER
Dim sngSize#
GetDiskFreeSpaceEx "c:\", lngFreeCaller, lngTotal, lngTotalFree
注释:以下用来显示出分区总容量(以G为单位)
MsgBox GetSize(lngTotal) / 2 ^ 30
End Sub
Private Function GetSize(lngSize As LARGE_INTEGER) As Single
注释:用来从LARGE_INTEGER型变量中换算出实际的大小
With lngSize
If .highpart < 0 Then
GetSize = (2 ^ 32 - 1 - .highpart) * (2 ^ 32 - 1)
Else
GetSize = .highpart * (2 ^ 32 - 1)
End If
If .lowpart < 0 Then
GetSize = GetSize + (2 ^ 32 - 1 - .lowpart)
Else
GetSize = GetSize + .lowpart
End If
End With
End Function
Private Function GetSize(lngSize As LARGE_INTEGER) As Single
注释:用来从LARGE_INTEGER型变量中换算出实际的大小
With lngSize
If .highpart < 0 Then
GetSize = (2 ^ 32 - 1 - .highpart) * (2 ^ 32 - 1)
Else
GetSize = .highpart * (2 ^ 32 - 1)
End If
If .lowpart < 0 Then
GetSize = GetSize + (2 ^ 32 - 1 - .lowpart)
Else
GetSize = GetSize + .lowpart
End If
End With
End Function
从LARGE_INTEGER的定义来看,应用此函数理论上可得到2^64/2^30=2^34G的分区的大小,不知这辈子能否用上这么大的硬盘.