VB.NET下擷取硬碟資訊的幾種方法

來源:互聯網
上載者:User
硬碟


1、用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、用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、用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、利用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.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命名空間)。
 
聲明:本文著作權與解釋權歸李洪根所有,如需轉載,請保留完整的內容及此聲明。
QQ: 21177563  
MSN: lihonggen@hotmail.com
專欄:http://www.csdn.net/develop/author/netauthor/lihonggen0/
 
 

 

Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=13650
[點擊此處收藏本文]   發表於 2004年03月09日 11:44 PM

 


皓翔 發表於2004-09-06 12:22 AM  IP: 220.192.67.*
以下是利用WMI擷取硬碟資訊的C#版
string strResult;

ManagementClass diskClass = new ManagementClass("Win32_LogicalDisk");

ManagementObjectCollection disks;
disks = diskClass.GetInstances();
foreach( ManagementObject disk in disks)
{
strResult = "";

strResult += "裝置ID:" + disk["DeviceID"];

strResult += "磁碟名稱:" + disk["Name"];

strResult += "磁碟卷標:" + disk["VolumeName"];

if( disk["FileSystem"].ToString() != "" )
{
strResult += "檔案系統:" + disk["FileSystem"];

strResult += "磁碟描述:" + disk["Description"];

if( System.Convert.ToInt64(disk["Size"]) > 0 )
{
strResult += "磁碟大小:" + System.Convert.ToInt64(disk["Size"].ToString());
}
strResult += "磁碟類型:" + System.Convert.ToInt16(disk["DriveType"].ToString());
}
Response.Write(strResult);
}
這個是C#的代碼,不過我遇到使用權限設定的問題,不知道如何設定,不知道那位GGMM幫忙解決一下System.Management.ImpersonationLevel
在web.config中加入這個也不行?
<system.web>
<identity impersonate="true" />
</system.web>
錯誤資訊:
異常詳細資料: System.Management.ManagementException: 訪問遭到拒絕 
 

 



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。