深入Scripting Runtime Library 之一
www.applevb.com
什么是Scripting Runtime Library?按照一般的说法,Scripting Runtime Library(以下简称SR)提供了微软 “忘记”放到Visual Basic中的基本的文件系统操作函数何对象。 点击菜单的 Project | Referrences 项,在References选择窗口的References列表中选择 Microsoft Scripting Runtime 项。然后单击 “确定”键选择退出就可以将Scripting Runtime Library加入到VB工程文件中。(在下面的程序中在 添加代码之前都要执行这一步操作,凡是在下面提到加入SR库,都是指这一步)一个SR对象包括FileSystemObject 对象和Directionary对象,分别对应文件系统操作和字典操作。 例如 Dim fsoSys As New Scripting.FileSystemObject 就定义了一个FileSystemObject对象 FileSystemObject对象包含获取驱动器信息的Drive对象;对文件进行复制、删除、移动等操作的File对象;对 文件夹进行建立、复制、删除、移动和获取文件夹下的文件和子文件夹的Folder对象;建立、读取、写入文本文件的 TextStream对象。下面对这些对象的属性和操作方法进行分门别类的介绍 一、对于驱动器Drive对象的操作 通过一个Drive对象可以获得该对象定义的驱动器的容量、属性等信息,使用FileSystemObject对象的GetDrive方 法可以得到一个Drive对象。 下面是一个Drive对象操作的范例。 首先在VB中建立一个新的工程。加入SR库。然后在Form1中加入一个ListBox控件、一个PictureBox不要改变它们 的属性,然后在Form1的代码窗口中加入以下代码: Option Explicit
Dim fsoSystem As New FileSystemObject Dim fsoDrives As Drives Dim fsoDrive As Drive
Private Sub Form_Load() Dim sDrive As String Dim sDriveType As String Dim bHasCDRom As Boolean
Set fsoDrives = fsoSystem.Drives For Each fsoDrive In fsoDrives sDrive = fsoDrive.DriveLetter & ": " Select Case fsoDrive.DriveType Case 0: sDriveType = "未知类型驱动器" Case 1: sDriveType = "可移动驱动器" Case 2: sDriveType = "固定驱动器" Case 3: sDriveType = "远程驱动器" Case 4: sDriveType = "CDROM驱动器" Case 5: sDriveType = "RAM Disk" End Select
If fsoDrive.DriveType = CDRom Or fsoDrive.DriveType = CDRom Then bHasCDRom = bHasCDRom Or fsoDrive.IsReady End If sDrive = sDrive & sDriveType List1.AddItem (sDrive) Next End Sub
Private Sub Form_Unload(Cancel As Integer) Set fsoSystem = Nothing End Sub
Private Sub List1_Click() Dim astr$ Dim fsoDrive As Drive If List1.ListIndex > -1 Then astr = Left$(List1.List(List1.ListIndex), 1) Set fsoDrive = fsoSystem.GetDrive(astr) '检查驱动器是否准备好 If Not fsoDrive.IsReady Then MsgBox ("该驱动器未准备好或未插入磁盘") Exit Sub End If '输出驱动器信息 With Picture1 .Cls .CurrentX = 30: .CurrentY = 30 Picture1.Print "总容量" + Format$(fsoDrive.TotalSize, _ "###,###,###,###,###,##0") + " 字节" Picture1.Print "可用容量" + Format$(fsoDrive.AvailableSpace, _ "###,###,###,###,###,##0") + " 字节" Picture1.Print fsoDrive.DriveLetter & ": 使用的文件系统为: " & _ fsoDrive.FileSystem End With Set fsoDrive = Nothing End If End Sub 运行程序,程序检测系统中所有的可用驱动器。然后将它们在List1上列出来,点击List1上的驱动器列表项, 该驱动器的基本信息就会显示在Picture1上,如果该驱动器未准备好(例如未插入磁盘或光盘),程序就会出现 提示。利用该方法可以检测软驱或者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: '如果文件创建时间为未知就会导致错误,这里是错误处理程序段 sDateCreate = "(未知)" Resume Next End Sub
Private Sub Form_Load() File1.Path = "c:\windows" End Sub
Private Sub Form_Unload(Cancel As Integer) Set fsoSys = Nothing End Sub
四、对于TextStream对象的操作 对于通过VB编程对文本文件进行IO操作,一直是一个头疼的问题。如果使用Inout$函数一次把打开的文件内容 全部读取到一个字符串中的话,对于某一行的字符串操作就会十分不方便,同时还会有一个字符串不能大于65K而导致 无法读取大文件的问题。而采用Line Input的方法又丧失了对文件整体操作的方便性。而TextStream对象提供了一种 基于流的文件操作方式,使得对文本文件的操作方便了很多。 利用FileSystemObject的CreateTextFile方法或者OpenAsTextStream 方法可以建立一个TextStream对象,该 对象包含了文件中的所有内容,可以通过只读、只写和追加方式打开文件。当建立了TextStream对象只后,用户就可以 直接对TextStream进行操作,从而增加了对文件操作的方便性和安全性。 下面是一个TextStream对象操作的范例。 首先建立一个新的工程文件,加入RS库,在Form1中加入三个CommandButon控件和一个TextBox控件,在C:\下建立 一个help.txt的文件,然后在Form1中加入以下代码: Option Explicit
Dim fsoFile As New FileSystemObject Dim fsoTextStream As TextStream
Private Sub Command1_Click() If Dir$("c:\help.txt") = "" Then MsgBox ("c:\help.txt文件不存在,程序将退出") Set fsoFile = Nothing End End If '打开文件 Set fsoTextStream = fsoFile.OpenTextFile("c:\help.txt", ForReading) Text1.Text = fsoTextStream.ReadAll '将文件读取到Text1中 End Sub
Private Sub Command2_Click() Dim fsoTextTemp As TextStream
'关闭原来的文件,并建立一个同名的新的文件,将更新的文件流写入到新文件中 fsoTextStream.Close Set fsoTextStream = Nothing Set fsoTextTemp = fsoFile.CreateTextFile("c:\help.txt", True) fsoTextTemp.Write Text1.Text fsoTextTemp.Close Set fsoTextTemp = Nothing Command1_Click Command2.Enabled = False End Sub
Private Sub Command3_Click() fsoTextStream.Close Set fsoTextStream = Nothing Set fsoFile = Nothing End End Sub
Private Sub Form_Load() Text1.Text = "" Command1.Caption = "打开文件" Command2.Caption = "保存文件" Command3.Caption = "退出" Command2.Enabled = False End Sub
Private Sub Text1_Change() Command2.Enabled = True End Sub 运行程序,点击Command1就可以打开c:\help.txt文件,按Command2保存文件,按Command3退出。 
|