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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。