深入Scripting Runtime Library 之一[2]

[入库:2005年8月18日] [更新:2007年3月24日]

本文简介:选择自 technofantasy 的 blog

提示。利用该方法可以检测软驱或者cd-rom驱动器中是否有盘以及实现向超级解霸中的cd自动检测功能。需要注意
的一点是:上面的程序在检测磁盘容量时只支持2gb的容量,也就是说如果你的分区大于2g的话,检测出来的容量
也不会超过2gb。

    二、对于文件夹folder对象的操作
    通过filesystemobject的getfolder方法可以获得一个folder对象。下面的范例介绍了如何建立一个folder对象
和利用该对象建立、删除文件夹和获取子文件夹的操作。
    首先建立一个工程文件,在其中加入sr库。在form1中加入一个treeview控件,两个commandbutton控件,然后
在form1中加入以下代码:
dim fsosys as new scripting.filesystemobject
dim fsorootfolder as folder

private sub form_load()
    dim fsosubfolder as folder
    dim nodrootnode as node
    dim nodchild as node
    dim astr$
   
    set nodrootnode = treeview1.nodes.add(, , "root", "c:\")
    set fsorootfolder = fsosys.getfolder("c:\")
    for each fsosubfolder in fsorootfolder.subfolders
        astr = fsosubfolder.path
        set nodchild = treeview1.nodes.add("root", tvwchild, astr, fsosubfolder.name)
    next
   
    set fsorootfolder = nothing
    command1.caption = "建立目录"
    command2.caption = "删除目录"
end sub

private sub form_unload(cancel as integer)
    set fsosys = nothing
end sub

private sub command1_click()
    dim fsofolder as folder
   
    '检查目录是否存在,如果目录不存在则建立新目录
    if fsosys.folderexists("c:\temp") then
        msgbox ("目录c:\temp已经存在,无法建立目录")
    else
        set fsofolder = fsosys.createfolder("c:\temp")
        set fsofolder = nothing
    end if
end sub

private sub command2_click()
    '检查目录是否存在,如存在则删除目录
    if fsosys.folderexists("c:\temp") then
        fsosys.deletefolder ("c:\temp")
    else
        msgbox ("目录c:\temp不存在")
    end if
end sub
    运行程序,程序建立一个指向c盘根目录的folder对象并获取它的所有子文件夹加入到treeview中,双击
treeview1中的 "c:\" 就可以打开分支查看c:\目录下的所有子目录名。点击command1就可以建立 c:\temp
目录,如果目录已存在程序会给出提示;点击command2删除c:\temp目录。

    三、对于文件file对象的操作
    通过filesystemobject的getfile方法可以建立一个指向磁盘上一个文件的file对象。下面的范例介绍了如何
利用file对象获得文件的基本信息。
    首先建立一个新的工程文件,加入sr库,在form1中加入一个filelistbox控件和一个picturebox控件,不要使
二者重叠,然后在form1中加入以下代码:
option explicit

dim fsosys as new scripting.filesystemobject

private sub file1_click()
    dim fsofile as file
    dim astr$
    dim sdatecreate
   
    on error goto errfun
    '获得file1中的文件名并据此建立一个file对象
    set fsofile = fsosys.getfile("c:\windows\" + file1.list(file1.listindex))
   
    picture1.cls
    picture1.currenty = 10
    picture1.print "文件名      " + fsofile.name
    picture1.print "dos文件名   " + fsofile.shortname
    picture1.print "文件类型    " + fsofile.type
    picture1.print "文件大小    " & str(fsofile.size)
    sdatecreate = fsofile.datecreated
    picture1.print "创建时间    " & sdatecreate
    picture1.print "修改时间    " & fsofile.datelastmodified
    picture1.print "访问时间    " & fsofile.datelastaccessed
    if fsofile.attributes and archive then
        astr = astr + "常规 "
    end if
    if fsofile.attributes and readonly then
        astr = astr + "只读 "
    end if
    if fsofile.attributes and hidden then
        astr = astr + "隐藏 "
    end if
    if fsofile.attributes and system then
        astr = astr + "系统 "
    end if
    if fsofile.attributes and compressed then
        astr = astr + "压缩 "
    end if
   
    picture1.print "文件类型    " + astr
   
    set fsofile = nothing
    exit sub
errfun:
    '如果文件创建时间为未知就会导致错误,这里是错误处理程序段

本文关键:深入Scripting Runtime Library 之一
  相关方案
Google
 

本站最佳浏览方式为 分辨率 1024x768 IE 6.0(或更高版本的 IE浏览器)

go top