1、用API函數GetDiskFreeSpaceEx獲取磁盤空間
Private Declare Function GetDiskFreeSpaceEx Lib "kernel32" Alias "GetDiskFreeSpaceExA"
_
(ByVal lpDirectoryName As String, ByRef lpFreeBytesAvailableToCaller As Long,
_
ByRef lpTotalNumberOfBytes As Long, ByRef lpTotalNumberOfFreeBytes As Long) As
Long
Private Sub btnDisk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnDisk.Click
Dim BytesFreeToCalller As Long, TotalBytes As Long
Dim TotalFreeBytes As Long, TotalBytesUsed As Long
Dim strResult As String
Const RootPathName = "C:\"
Call GetDiskFreeSpaceEx(RootPathName, BytesFreeToCalller, TotalBytes, TotalFreeBytes)
strResult = " Drive " & "C:\" & vbCrLf
strResult += "磁盤容量(MB):" & Format(CDbl((TotalBytes / 1024) / 1024),
"###,###,##0.00") & vbCrLf
strResult += "可用空間(MB):" & Format(CDbl((TotalFreeBytes / 1024) /
1024), "###,###,##0.00") & vbCrLf
strResult += "已用空間(MB):" & Format(CDbl(((TotalBytes - TotalFreeBytes)
/ 1024) / 1024), "###,###,##0.00") & vbCrLf
MsgBox(strResult)
End Sub
2、用FSO(文件系統對象模型)實現 FSO對象模型包含在SCRIPTING類型庫(SCRRUN.DLL)中。調用方法如下:
在項目菜單中選擇引用,在COM中選擇Microsoft Scripting Runtime
在代碼最頂端添加Imports Scripting,在按鈕的單擊事件中加入以下代碼:
Imports Scripting
Private Sub btnFso_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles btnFso.Click
Dim Fso As New FileSystemObject
Dim drvDisk As Drive, strResult As String
drvDisk = Fso.GetDrive("C:\")
strResult = "Drive " & "C:\" & vbCrLf
strResult += "磁盤卷標:" & drvDisk.VolumeName & vbCrLf
strResult += "磁盤序列號:" & drvDisk.SerialNumber & vbCrLf
strResult += "磁盤類型:" & drvDisk.DriveType & vbCrLf
strResult += "文件系統:" & drvDisk.FileSystem & vbCrLf
strResult += "磁盤容量(G): " & FormatNumber(((drvDisk.TotalSize / 1024)
/ 1024) / 1024, 2, , , Microsoft.VisualBasic.TriState.True) & vbCrLf
strResult += "可用空間(G): " & FormatNumber(((drvDisk.FreeSpace / 1024)
/ 1024) / 1024, 2, , , Microsoft.VisualBasic.TriState.True) & vbCrLf
strResult += "已用空間(G):" & FormatNumber(((((drvDisk.TotalSize - drvDisk.FreeSpace)
/ 1024) / 1024) / 1024), 2, , , Microsoft.VisualBasic.TriState.True)
MsgBox(strResult)
End Sub
3、用API函數GetVolumeInformation獲取邏輯盤序列號 Private Declare Function GetVolumeInformation Lib "kernel32" Alias "GetVolumeInformationA"
_
(ByVal lpRootPathName As String, ByVal lpVolumeNameBuffer As String, ByVal _
nVolumeNameSize As Integer, ByRef lpVolumeSerialNumber As Long, _
ByVal lpMaximumComponentLength As Integer, ByVal lpFileSystemFlags As Integer,
ByVal _
lpFileSystemNameBuffer As String, ByVal nFileSystemNameSize As Integer) As Integer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button2.Click
Dim SerialNumber As Long
Dim TempStr1 As New String(Chr(0), 255)
Dim TempStr2 As New String(Chr(0), 255)
Dim TempInt1, TempInt2 As Integer
GetVolumeInformation("C:\", TempStr1, 256, SerialNumber, TempInt1, TempInt2,
TempStr2, 256)
MsgBox("C盤序列號:" & SerialNumber)
End Sub
4、利用WMI獲取硬盤信息 Windows Management Instrumentation (WMI) 是可伸縮的系統管理結構,它采用一個統一的、基於標准的、可擴展的面向對象接口。WMI 為您提供與系統管理信息和基礎 WMI API 交互的標准方法。WMI 主要由系統管理應用程序開發人員和管理員用來訪問和操作系統管理信息。
我們需要使用.Net Framwork裡面System.Management命名空間下提供的類來實現。
Imports System.Management
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button3.Click
Dim disk As ManagementBaSEObject
Dim strResult As String
Dim diskClass = New ManagementClass("Win32_LogicalDisk")
Dim disks As ManagementObjectCollection
disks = diskClass.GetInstances()
For Each disk In disks
strResult = ""
strResult += "設備ID:" & disk("DeviceID") & vbCrLf
strResult += "磁盤名稱:" & disk("Name") & vbCrLf
strResult += "磁盤卷標:" & disk("VolumeName") & vbCrLf
If disk("FileSystem") <> "" Then strResult += "文件系統:"
& disk("FileSystem") & vbCrLf
strResult += "磁盤描述:" & disk("Description") & vbCrLf
If System.Convert.ToInt64(disk("Size")) > 0 Then
strResult += "磁盤大小:" & System.Convert.ToInt64(disk("Size").ToString())
& vbCrLf
strResult += "磁盤類型:" & System.Convert.ToInt16(disk("DriveType").ToString())
End If
MsgBox(strResult)
Next
End Sub
總結:在VB.NET中,用API函數可以獲取硬盤信息。原來熟悉API函數VB6程序員,可以對API函數聲明進行適當的更改後,進行調用。利用FSO(文件系統對象)的Scrrun.DLL,也可以獲得磁盤信息。在.Net Framwork中,利用WMI可以獲取更多的關於機器硬件的詳細信息(參考System.Management命名空間)。