您的位置:程序门 -> vb ->



vb中获取逻辑磁盘的信息


[收藏此页] [打印本页]选择字色:背景色:字体:[][][]


vb中获取逻辑磁盘的信息
发表于:2007-12-14 19:18:28 楼主
我们在编程的时候有时会需要得到系统中逻辑磁盘的一些信息,如磁盘卷标、磁盘序列号、空间大小、剩余空间等,这些信息直接使用vb提供的函数显然是无法得到的。但是,借助于vb对windows   api函数的支持,使用getvolumeinformation和   getdiskfreespace这两个api函数,我们就可以很容易的得到磁盘的相关信息。  

  先来谈谈这两个函数。getvolumeinformation函数用于获取与一个磁盘卷有关的信息,包括磁盘卷标、磁盘的序列号、文件的全路径名中“\”与“\”之间部分的长度、文件系统的名称以及文件系统的某些特性。getdiskfreespace函数用于获取与一个磁盘组织有关的信息,以及了解剩余空间的容量,包括磁盘上的总簇数、剩余簇数、一个簇内的扇区数和一个扇区内的字节数。  

  接下来看看具体的例子。  

  进入vb中,在窗体上加入一个驱动器列表框(drivelistbox)和一个列表框(listbox),然后加入以下的脚本:  

option   explicit
private   declare   function   getvolumeinformation  
lib   "kernel32"   alias  
"getvolumeinformationa"   (byval   lprootpathname   as  
string,   byval   lpvolumenamebuffer   as  
string,   byval   nvolumenamesize   as   long,
lpvolumeserialnumber   as   long,  
lpmaximumcomponentlength   as   long,
lpfilesystemflags   as   long,   byval  
lpfilesystemnamebuffer   as   string,  
byval   nfilesystemnamesize   as   long)   as   long
private   declare   function   getdiskfreespace  
lib   "kernel32"   alias   "getdiskfreespacea"  
(byval   lprootpathname   as   string,   lpsectorspercluster
as   long,   lpbytespersector   as   long,  
lpnumberoffreeclusters   as   long,
lptotalnumberofclusters   as   long)   as   long
private   const   fs_case_is_preserved   =   &h2
private   const   fs_case_sensitive   =   &h1
private   const   fs_unicode_stored_on_
disk   =   &h4
private   const   fs_persistent_acls   =   &h8
private   const   fs_file_compression   =   &h10
private   const   fs_vol_is_compressed   =  
&h8000

private   sub   drive1_change()
dim   volume   as   string,   sysname   as   string
dim   serialnum   as   long,   sysflags   as   long,  
componentlength   as   long,   res   as   long
dim   sectorspercluster   as   long,   bytespersector  
as   long,   numberoffreeclustors   as  
long,   totalnumberofclustors   as   long
dim   freebytes   as   long,   totalbytes   as   long,  
percentfree   as   long,   dl   as   long
dim   drvname   as   string
        list1.clear
        volume   =   string(256,   0)
        sysname   =   string(256,   0)
        drvname   =   left(drive1.drive,   2)   &   "\"
res   =   getvolumeinformation(drvname,  
volume,   255,   serialnum,   _
        componentlength,   sysflags,   sysname,   255)
        if   res   =   0   then
                list1.additem   "不能得到磁盘信息"
        else
                list1.additem   "卷标:   "   &   trim(volume)
list1.additem   "序列号:   "   &   serialnum
list1.additem   "成分长度:   "   &   componentlength
list1.additem   "文件系统:   "   &   trim(sysname)
dl   =   getdiskfreespace(drvname,  
sectorspercluster,   bytespersector,  
numberoffreeclustors,   totalnumberofclustors)
                list1.additem   "每簇中扇区数:   "  
            &   format(sectorspercluster,   "#,0")
                list1.additem   "每扇区中字节数:   "
            &   format(bytespersector,   "#,0")
                list1.additem   "总簇数:   "  
                &   format(totalnumberofclustors,   "#,0")
                list1.additem   "剩余簇数:   "  
                &   format(numberoffreeclustors,   "#,0")
                totalbytes   =   totalnumberofclustors   *  
        sectorspercluster   *   bytespersector
                list1.additem   "总字节数:  
            "   &   format(totalbytes,   "#,0")
                freebytes   =   numberoffreeclustors
*   sectorspercluster   *   bytespersector
                list1.additem   "剩余字节数:   "  
&   format(freebytes,   "#,0")
if   sysflags   and   fs_case_is_preserved   then
list1.additem   "文件名的大小写记录于文件系统"
                end   if
                if   sysflags   and   fs_case_sensitive   then
                        list1.additem   "文件名要区分大小写"
                end   if
                if   sysflags   and   fs_unicode_stored_
on_disk   then
                        list1.additem   "文件名保存为   unicode   格式"
                end   if
                if   sysflags   and   fs_persistent_acls   then
                        list1.additem   "文件系统支持文件的访问
            控制列表(acl)安全机制"
                end   if
                if   sysflags   and   fs_file_compression   then
            list1.additem   "文件系统支持逐文件地进行文件压缩"
                end   if
                if   sysflags   and   fs_vol_is_compressed   then
                        list1.additem   "整个磁盘卷都是压缩的"
                end   if
        end   if
end   sub

private   sub   form_load()
        call   drive1_change
end   sub

  运行后,选择驱动器列表框中的不同驱动器,列表框中就会显示出该驱动器的相应信息。以上程序在vb5.0、vb6.0及windows   98中运行通过。  
发表于:2007-12-14 19:34:091楼 得分:0
jf,已作收藏.


快速检索

最新资讯
热门点击