嘻嘻!分,我来了!检查硬盘空间
使用API函数可以方便的计算出指定硬盘的总空间和剩余空间.其函数声明为:
Long GetDiskFreeSpace(
lpRootPathName as string,'指定驱动器盘符,如 C:
lpSectorsPerCluster as long, 每串的扇区数
lpBytesPerSector as long,'每扇区的字节数
lpNumberOfFreeClusters as long,'剩余串数
lpTotalNumberOfClusters as long'总串数
);
>>步骤1----建立新工程,在窗体上放置一个CommandButton按钮.
>>步骤2----加入如下代码:
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 freeSpace As Long, totalSpace As Long
Dim lpRootPathName As String, lpSectorsPerCluster As Long, _
lpBytesPerSector As Long, lpNumberOfFreeClusters As _
Long,lpTotalNumberOfClusters As Long
lpRootPathName = "C:"
freeSpace = GetDiskFreeSpace(lpRootPathName, _
lpSectorsPerCluster,lpBytesPerSector, _
lpNumberOfFreeClusters,lpTotalNumberOfClusters)
If freeSpace <> 0 Then
freeSpace = lpSectorsPerCluster * lpBytesPerSector * _
lpNumberOfFreeClusters
totalSpace = lpSectorsPerCluster * lpBytesPerSector * _
lpTotalNumberOfClusters
Else
freeSpace = -1
totalSpace = -1
End If
MsgBox lpRootPathName & "的剩余空间为:" & Fix(freeSpace _
/ 1000000) & "MB" & Chr(10) _
& "总空间为:" & Fix(totalSpace / 100000000) / 10 & "GB", _
vbInformation, "硬盘空间"
End Sub >>步骤3----编译运行,出来了吧.
使用API函数可以方便的计算出指定硬盘的总空间和剩余空间.其函数声明为:
Long GetDiskFreeSpace(
lpRootPathName as string,'指定驱动器盘符,如 C:
lpSectorsPerCluster as long, 每串的扇区数
lpBytesPerSector as long,'每扇区的字节数
lpNumberOfFreeClusters as long,'剩余串数
lpTotalNumberOfClusters as long'总串数
);
>>步骤1----建立新工程,在窗体上放置一个CommandButton按钮.
>>步骤2----加入如下代码:
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 freeSpace As Long, totalSpace As Long
Dim lpRootPathName As String, lpSectorsPerCluster As Long, _
lpBytesPerSector As Long, lpNumberOfFreeClusters As _
Long,lpTotalNumberOfClusters As Long
lpRootPathName = "C:"
freeSpace = GetDiskFreeSpace(lpRootPathName, _
lpSectorsPerCluster,lpBytesPerSector, _
lpNumberOfFreeClusters,lpTotalNumberOfClusters)
If freeSpace <> 0 Then
freeSpace = lpSectorsPerCluster * lpBytesPerSector * _
lpNumberOfFreeClusters
totalSpace = lpSectorsPerCluster * lpBytesPerSector * _
lpTotalNumberOfClusters
Else
freeSpace = -1
totalSpace = -1
End If
MsgBox lpRootPathName & "的剩余空间为:" & Fix(freeSpace _
/ 1000000) & "MB" & Chr(10) _
& "总空间为:" & Fix(totalSpace / 100000000) / 10 & "GB", _
vbInformation, "硬盘空间"
End Sub >>步骤3----编译运行,出来了吧.
Attribute VB_Name = "Module1"
Public Declare Function SHGetDiskFreeSpace Lib "shell32" _
Alias "SHGetDiskFreeSpaceA" _
(ByVal lpszVolume As String, _
cCall As Currency, _
cTotal As Currency, _
cFree As Currency) As Long2 然后窗体form1
VERSION 5.00
Begin VB.Form Form1
BorderStyle = 1 'Fixed Single
Caption = "Disk Space"
ClientHeight = 2715
ClientLeft = 3975
ClientTop = 2010
ClientWidth = 3705
LinkTopic = "Form1"
MaxButton = 0 'False
MinButton = 0 'False
ScaleHeight = 2715
ScaleWidth = 3705
Begin VB.TextBox Text3
Height = 330
Left = 1500
TabIndex = 3
Top = 1440
Width = 2010
End
Begin VB.DriveListBox Drive1
Height = 315
Left = 1500
TabIndex = 0
Top = 180
Width = 2010
End
Begin VB.TextBox Text2
Height = 330
Left = 1500
TabIndex = 2
Top = 1015
Width = 2010
End
Begin VB.TextBox Text1
Height = 330
Left = 1500
TabIndex = 1
Top = 590
Width = 2010
End
Begin VB.CommandButton Command1
Caption = "Check"
Default = -1 'True
Height = 435
Left = 1215
TabIndex = 4
Top = 2040
Width = 1275
End
Begin VB.Label Label4
Caption = "Available Space:"
Height = 225
Left = 210
TabIndex = 8
Top = 1500
Width = 1365
End
Begin VB.Label Label3
Caption = "Free Space:"
Height = 225
Left = 210
TabIndex = 7
Top = 1070
Width = 1065
End
Begin VB.Label Label2
Caption = "Total Space:"
Height = 225
Left = 210
TabIndex = 6
Top = 640
Width = 1065
End
Begin VB.Label Label1
Caption = "Drive:"
Height = 225
Left = 210
TabIndex = 5
Top = 210
Width = 855
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
Dim cCall As Currency
Dim cTotal As Currency
Dim cFree As Currency
SHGetDiskFreeSpace Drive1.Drive, _
cCall, _
cTotal, _
cFree
Text1.Text = Format(cTotal * 10000, "###,###,###,##0")
Text2.Text = Format(cFree * 10000, "###,###,###,##0")
Text3.Text = Format(cCall * 10000, "###,###,###,##0")
End Sub
Dim fs As Object, d As Object
Set fs = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
For Each d In fs.Drives
Debug.Print d.driveletter & ": 总空间:" & d.TotalSize & "字节,剩余空间:" & d.FreeSpace & "字节"
Next
End Sub