您好,登錄后才能下訂單哦!
VB.NET中怎么獲取硬盤信息,相信很多沒有經驗的人對此束手無策,為此本文總結了問題出現的原因和解決方法,通過這篇文章希望你能解決這個問題。
VB.NET獲取硬盤信息1、用API函數GetDiskFreeSpaceEx獲取磁盤空間
Private Declare Function GetDiskFree
SpaceEx 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
VB.NET獲取硬盤信息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.ClickDim Fso As New FileSystemObject
Dim drvDisk As Drive,
strResult As StringdrvDisk = Fso.GetDrive("C:\")
strResult = "Drive " &
"C:\" & vbCrLfstrResult += "磁盤卷標:" &
drvDisk.VolumeName & vbCrLfstrResult += "磁盤序列號:" &
drvDisk.SerialNumber & vbCrLfstrResult += "磁盤類型:" &
drvDisk.DriveType & vbCrLfstrResult += "文件系統:" &
drvDisk.FileSystem & vbCrLfstrResult += "磁盤容量(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
VB.NET獲取硬盤信息3、用API函數GetVolumeInformation獲取邏輯盤序列號
Private Declare Function GetVolume
Information 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 IntegerPrivate 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
VB.NET獲取硬盤信息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.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") & vbCrLfstrResult += "磁盤名稱:" & disk("Name")
& vbCrLfstrResult += "磁盤卷標:" & disk("VolumeName")
& vbCrLfIf disk("FileSystem") <> "" Then strResult
+= "文件系統:" & disk("FileSystem") & vbCrLfstrResult += "磁盤描述:" & disk
("Description") & vbCrLfIf System.Convert.ToInt64(disk("Size"))
> 0 ThenstrResult += "磁盤大小:" & System.
Convert.ToInt64(disk("Size").ToString())
& vbCrLfstrResult += "磁盤類型:" & System.Convert.
ToInt16(disk("DriveType").ToString())End If
MsgBox(strResult)
Next
End Sub
看完上述內容,你們掌握VB.NET中怎么獲取硬盤信息的方法了嗎?如果還想學到更多技能或想了解更多相關內容,歡迎關注億速云行業資訊頻道,感謝各位的閱讀!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。