实例说明
在本程序中,我们将生成一个能够得到当前磁盘信息的应用程序,跟右键点击磁盘"属性"得到的结果相似。运行后,选择磁盘后,可以得到当前磁盘的序列号、卷标、文件类型、已用空间、未用空间、磁盘容量等信息。程序运行结果如图96-1所示。
图96-1 运行结果
技术要点
l 判断驱动器类型
l 取得磁盘的相关信息
l 取得磁盘的使用空间、总空间
实现过程
■ 新建项目
打开Visual Studio.NET,选择"新建项目",在项目类型窗口中选择"Visual Basic项目",在模板窗口中选择"Windows应用程序",在名称域中输入"DiskInfo",然后选择保存路径。单击"确认"。
■ 添加控件
向当前窗体上添加十六个Label控件,一个Picture控件,一个ComboBox控件,一个Line控件。
■ 设置属性
根据窗体的界面,将说明性的Label控件的Text属性改为和界面一致即可。在表96-1中列出了一些主要属性,其余属性读者可以参考光盘。
表96-1 窗体及控件的属性值
窗体/控件 属性 值
Label10 Text
BackColor &H000000FF
Label11 Text
Value &H00FF0000
其他Label控件 Autosize True
Text 跟界面一致
Option Strict Off
Module Module1
Public Declare Function GetDriveType Lib "kernel32" Alias "GetDriveTypeA" (ByVal nDrive As String) As Integer
Public Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA" (ByVal lpRootPathName As String,ByVal lpVolumeNameBuffer As String,ByVal nVolumeNameSize As Integer,ByRef lpVolumeSerialNumber As Integer,ByRef lpMaximumComponentLength As Integer,ByRef lpFileSystemFlags As Integer,ByVal lpFileSystemNameBuffer As String,ByVal nFileSystemNameSize As Integer) As Integer
Public Declare Function GetDiskFreeSpace Lib "kernel32" Alias "GetDiskFreeSpaceA" (ByVal lpRootPathName As String,ByRef lpSectorsPerCluster As Integer,ByRef lpBytesPerSector As Integer,ByRef lpNumberOfFreeClusters As Integer,ByRef lpTotalNumberOfClusters As Integer) As Integer
End Module
Private Sub Combo1_SelectedIndexChanged(ByVal eventSender As System.Object,ByVal eventArgs As System.EventArgs) Handles Combo1.SelectedIndexChanged
Dim buff As String
Dim retSerial,retRoot,retVolume1,retFlag As String
Dim retFSNbuffer As String
Dim lpBytesPerSector,lppercluster,lpfree As Integer
Dim lptotal As Integer
Dim freespace As Long
Dim totalspace As Double
Dim usespace As Double
Dim startang As Single
Dim endang As Single
Dim x As Integer
Dim retMaxLength As Integer
On Error Resume Next
buff = Combo1.Text & ":/"
' 判断磁盘驱动器类型
x = GetDriveType(buff)
Select Case x
Case 2
lbldrivetype.Text = " 软盘驱动器"
Case 3
lbldrivetype.Text = "硬盘驱动器 "
Case 4
lbldrivetype.Text = " 网络驱动器"
Case 5
lbldrivetype.Text = " CD - ROM驱动器"
Case 6
lbldrivetype.Text = " RAMDISK驱动器"
Case Else
lbldrivetype.Text = " "
End Select
' 得到卷标名称
retVolume1 = New String(Chr(0),255)
retFSNbuffer = New String(Chr(0),255)
x = GetVolumeInformation(buff,Len(retVolume1),retSerial,retMaxLength,retFlag,retFSNbuffer,Len(retFSNbuffer))
'retVolume = retVolume & Chr(0)
lblFileSystem.Text = retFSNbuffer
lblvolume.Text = retVolume1
If lblvolume.Text = "" Then
lblvolume.Text = "无卷标"
End If
' 计算磁盘剩余空间、已用空间和总空间
x = GetDiskFreeSpace(buff,lpBytesPerSector,lpfree,lptotal)
freespace = lpfree * lpBytesPerSector * lppercluster
totalspace = lptotal * lpBytesPerSector * CDbl(lppercluster)
'totalspace = totalspace * 10
lblUnusedSpace.Text = CStr(freespace) & "字节 " & CStr(Int(freespace / 1024 / 1024)) & " MB"
lblTotalVolume.Text = CStr(totalspace) & " 字节 " & CStr(Int(totalspace / 1024 / 1024)) & " MB"
lblUsedSpace.Text = CStr(totalspace - freespace) & " 字节 " & CStr(Int((totalspace - freespace) / 1024 / 1024)) & " MB"
lblintro.Text = "驱动器" & Combo1.Text
End Sub
' 窗体启动时,自动将当前系统上的磁盘添加到Combo控件中
Private Sub Form1_Load(ByVal eventSender As System.Object,ByVal eventArgs As System.EventArgs) Handles MyBase.Load
Dim x As Integer
Dim i As Short
Dim buff As String
For i = 65 To 90
buff = Chr(i) & ":/"
x = GetDriveType(buff)
If x > 1 Then
Combo1.Items.Add(Chr(i))
End If
Next
Combo1.Text = "C"
Call Combo1_SelectedIndexChanged(Combo1,New System.EventArgs())
End Sub
■ 运行程序
单击菜单"调试|启动"或单击 图标运行程序。
小结
我们通过使用这三个GetDriveType(),etVolumeInformation(),etDiskFreeSpace()API函数来得到有关磁盘的信息。其中,GetDriveType()函数用于获得有关磁盘驱动器类型的信息;GetVolumeInformation()函数用于获得磁盘卷标的一些信息;GetDiskFreeSpace()用于获得磁盘剩余空间和总空间信息。