vb.net下獲取硬盤信息的幾種方法
1、用api函數(shù)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(文件系統(tǒng)對(duì)象模型)實(shí)現(xiàn)
fso對(duì)象模型包含在scripting類型庫(scrrun.dll)中。調(diào)用方法如下:
在項(xiàng)目菜單中選擇引用,在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 += "磁盤卷標(biāo):" & drvdisk.volumename & vbcrlf
strresult += "磁盤序列號(hào):" & drvdisk.serialnumber & vbcrlf
strresult += "磁盤類型:" & drvdisk.drivetype & vbcrlf
strresult += "文件系統(tǒng):" & 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函數(shù)getvolumeinformation獲取邏輯盤序列號(hào)
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盤序列號(hào):" & serialnumber)
end sub
4、利用wmi獲取硬盤信息
windows management instrumentation (wmi) 是可伸縮的系統(tǒng)管理結(jié)構(gòu),它采用一個(gè)統(tǒng)一的、基于標(biāo)準(zhǔn)的、可擴(kuò)展的面向?qū)ο蠼涌凇mi 為您提供與系統(tǒng)管理信息和基礎(chǔ) wmi api 交互的標(biāo)準(zhǔn)方法。wmi 主要由系統(tǒng)管理應(yīng)用程序開發(fā)人員和管理員用來訪問和操作系統(tǒng)管理信息。
我們需要使用.net framwork里面system.management命名空間下提供的類來實(shí)現(xiàn)。
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 += "設(shè)備id:" & disk("deviceid") & vbcrlf
strresult += "磁盤名稱:" & disk("name") & vbcrlf
strresult += "磁盤卷標(biāo):" & disk("volumename") & vbcrlf
if disk("filesystem") <> "" then strresult += "文件系統(tǒng):" & 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
總結(jié):在vb.net中,用api函數(shù)可以獲取硬盤信息。原來熟悉api函數(shù)vb6程序員,可以對(duì)api函數(shù)聲明進(jìn)行適當(dāng)?shù)母暮螅M(jìn)行調(diào)用。利用fso(文件系統(tǒng)對(duì)象)的scrrun.dll,也可以獲得磁盤信息。在.net framwork中,利用wmi可以獲取更多的關(guān)于機(jī)器硬件的詳細(xì)信息(參考system.management命名空間)。
聲明:本文版權(quán)與解釋權(quán)歸李洪根所有,如需轉(zhuǎn)載,請(qǐng)保留完整的內(nèi)容及此聲明。
qq: 21177563
msn: [email protected]
專欄:http://www.csdn.net/develop/author/netauthor/lihonggen0/
新聞熱點(diǎn)
疑難解答
圖片精選