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: [email protected]
專欄: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: 訪問遭到拒絕 
 
新聞熱點
疑難解答
圖片精選