您好,登錄后才能下訂單哦!
VB.NET中如何獲取硬盤信息,很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
1、VB.NET獲取硬盤信息之一:用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、VB.NET獲取硬盤信息之二:用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.ClickDim 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) & vbCrLfstrResult += "可用空間(G): " & FormatNumber(((drvDisk.FreeSpace / 1024) / 1024) / 1024, 2,
Microsoft.VisualBasic.TriState.True) & vbCrLfstrResult += "已用空間(G):" & FormatNumber(((((drvDisk.TotalSize - drvDisk.FreeSpace) /
1024) / 1024) / 1024), 2, , , Microsoft.VisualBasic.TriState.True)MsgBox(strResult)
End Sub
3、VB.NET獲取硬盤信息之三:用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.ClickDim 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、VB.NET獲取硬盤信息之四:利用WMI獲取硬盤信息
Windows Management Instrumentation (WMI) 是可伸縮的系統管理結構,它采用一個統一的、基于標準的、可擴展的面向對象接口。WMI 為您提供與系統管理信息和基礎 WMI API 交互的標準方法。WMI 主要由系統管理應用程序開發人員和管理員用來訪問和操作系統管理信息。我們需要使用.net Framwork里面System.Management命名空間下提供的類來實現。
Private Sub Button3_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button3.ClickDim 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
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。