VB.NET还是比较常用的,于是我研究了一下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.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、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.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、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.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命名空间)。
【编辑推荐】
网站名称:VB.NET获取硬盘信息四大法宝
本文路径:http://www.shufengxianlan.com/qtweb/news8/436908.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联