国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

VB.NET獲取硬盤信息的幾種方法

2024-07-10 13:01:25
字體:
供稿:網(wǎng)友


收集最實(shí)用的網(wǎng)頁特效代碼!


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/






發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 清徐县| 卢龙县| 宁陕县| 松江区| 五峰| 自治县| 上林县| 姚安县| 麟游县| 山阴县| 唐山市| 邵阳市| 宁陕县| 黑龙江省| 巴彦淖尔市| 桂平市| 海安县| 区。| 武功县| 精河县| 平湖市| 电白县| 西吉县| 白银市| 巫溪县| 聂拉木县| 深泽县| 盈江县| 东方市| 炎陵县| 临朐县| 阜南县| 句容市| 化德县| 会理县| 宿松县| 年辖:市辖区| 广平县| 海宁市| 蕲春县| 家居|