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/ 10242, , , Microsoft.VisualBasic.TriState.True) & vbCrLf

        strResult 
+= "可用空间(G): " & FormatNumber(((drvDisk.FreeSpace / 1024/ 1024/ 10242, , , Microsoft.VisualBasic.TriState.True) & vbCrLf

        strResult 
+= "已用空间(G):" & FormatNumber(((((drvDisk.TotalSize - drvDisk.FreeSpace) / 1024/ 1024/ 1024), 2, , , Microsoft.VisualBasic.TriState.True)

        
MsgBox(strResult)

    
End Sub


转自 中国.NET论坛
原文地址:https://www.cnblogs.com/tatsuya/p/661612.html