您好,登錄后才能下訂單哦!
VB.NET中怎么獲取硬盤信息,相信很多沒(méi)有經(jīng)驗(yàn)的人對(duì)此束手無(wú)策,為此本文總結(jié)了問(wèn)題出現(xiàn)的原因和解決方法,通過(guò)這篇文章希望你能解決這個(gè)問(wèn)題。
VB.NET獲取硬盤信息1、用API函數(shù)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(文件系統(tǒng)對(duì)象模型)實(shí)現(xiàn)
FSO對(duì)象模型包含在SCRIPTING類型庫(kù)(SCRRUN.DLL)中。調(diào)用方法如下:
在項(xiàng)目菜單中選擇引用,在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 += "磁盤卷標(biāo):" &
drvDisk.VolumeName & vbCrLfstrResult += "磁盤序列號(hào):" &
drvDisk.SerialNumber & vbCrLfstrResult += "磁盤類型:" &
drvDisk.DriveType & vbCrLfstrResult += "文件系統(tǒng):" &
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函數(shù)GetVolumeInformation獲取邏輯盤序列號(hào)
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盤序列號(hào):" & SerialNumber)
End Sub
VB.NET獲取硬盤信息4、利用WMI獲取硬盤信息
Windows Management Instrumentation (WMI) 是可伸縮的系統(tǒng)管理結(jié)構(gòu),它采用一個(gè)統(tǒng)一的、基于標(biāo)準(zhǔn)的、可擴(kuò)展的面向?qū)ο蠼涌?。WMI 為您提供與系統(tǒng)管理信息和基礎(chǔ) WMI API 交互的標(biāo)準(zhǔn)方法。WMI 主要由系統(tǒng)管理應(yīng)用程序開(kāi)發(fā)人員和管理員用來(lái)訪問(wèn)和操作系統(tǒng)管理信息。
我們需要使用.net Framwork里面System.Management命名空間下提供的類來(lái)實(shí)現(xiàn)。
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 += "設(shè)備ID:" & disk
("DeviceID") & vbCrLfstrResult += "磁盤名稱:" & disk("Name")
& vbCrLfstrResult += "磁盤卷標(biāo):" & disk("VolumeName")
& vbCrLfIf disk("FileSystem") <> "" Then strResult
+= "文件系統(tǒng):" & 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
看完上述內(nèi)容,你們掌握VB.NET中怎么獲取硬盤信息的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。