Board logo

标题: 如何求出硬盘大小及剩余空间大小 [打印本页]

作者: cnangel    时间: 2002-10-27 02:11     标题: 如何求出硬盘大小及剩余空间大小

在我们安装软体的时候,在安装选项的画面,常常会出现如下的一些叙述:
选择安装项目大小..............................................10,000,000 Bytes
C 硬盘总空间大小..........................................1,847,328,768 Bytes
C 硬盘剩余空间大小...........................................51,707,904 Bytes
后面的二项是我们硬盘的资讯,我们只要使用一个 API,就可以同时抓到这二个资讯!
请在声明区中放入以下声明:
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 Sub Command1_Click()
Dim SectorsPerCluster As Long '参数二:每个 Cluster 的 Sector 数
Dim BytesPerSector As Long '参数三:每个 Sector 的 Byte 数
Dim NumberOfFreeClusters As Long '参数四:剩余的 Cluster 数
Dim TotalNumberOfClusters As Long '参数五:Cluster 总数
Dim FreeBytes As Long '剩余的 Byte 数
Dim TotalBytes As Long '总 Byte 数
Dim dummy As Long '传回值
dummy = GetDiskFreeSpace("c:\", SectorsPerCluster, BytesPerSector, NumberOfFreeClusters, TotalNumberOfClusters)
FreeBytes = NumberOfFreeClusters * SectorsPerCluster * BytesPerSector
TotalBytes = TotalNumberOfClusters * SectorsPerCluster * BytesPerSector
剩余空间大小 = FreeBytes
硬盘大小 = TotalBytes
End Sub
注:在 VB6 以前的各版本 VB,只能使用这种方法来做,但是到了 VB6 已经有了更简单、不要使用 API 的新作法,就是使用新物件 FileSystemObject.







欢迎光临 星星博客 (http://commerce.huhoo.net/) Powered by Discuz! 7.0.0