首页 > 编程 > .NET > 正文

VB.NET下获取硬盘信息的几种方法

2024-07-10 13:07:08
字体:
来源:转载
供稿:网友


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: 访问遭到拒绝 
 

 

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表