深入Scripting Runtime Library

  什么是 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:emp") Then
MsgBox ("目录c:emp已经存在,无法建立目录")
Else
Set fsoFolder = fsoSys.CreateFolder("c:emp")
Set fsoFolder = Nothing
End If

End Sub

Private Sub Command2_Click() '检查目录是否存在,如存在则删除目录

If fsoSys.FolderExists("c:emp") Then
fsoSys.DeleteFolder ("c:emp")
Else
MsgBox ("目录c:emp不存在")
End If

End Sub


运行程序,程序建立一个指向 C 盘根目录的 Folder 对象并获取它的所有子文件夹加入到 TreeView 中,双击 TreeView1 中的 "c:" 就可以打开分支查看 c: 目录下的所有子目录名。点击 Command1 就可以建立 c:emp 目录,如果目录已存在程序会给出提示;点击 Command2 删除 c:emp 目录。

三、对于文件 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 退出。

接下来要介绍的是Scripting Runtime Library一个不起眼但是比较有用的对象—Dictionary对象

如果在VB中要实现象字典一样一一对应的列表并实现查询,例如实现学生学号同学生姓名一一对应,输入学号就可以得到学生姓名这样的程序。你会使用什么什么方法呢?一般的方法是建立两个列表,分别在两个列表中输入学生姓名和学号,在查询时首先查询学号列表中相符合的学号,然后在学生姓名列表中调出相应的学生姓名。但是这样做编程的量增大,而且不便于维护。还有的读者可能想到了使用数据库,但是这样做显得有一些杀鸡用牛刀,而且调用数据库对于程序的尺寸和运行速度有很大影响。这里我要象大家介绍的是利用Scripting Runtime Library中的Dictionary对象实现字典功能。
下面首先通过程序来介绍Dictionary对象的使用首先建立一个新的工程文件,在Form1中加入一个ListBox控件,一个TextBox控件,然后在Form1的代码窗口中加入以下代码:

Dim dicTemp As New Scripting.Dictionary

Private Sub Form_Load()
List1.AddItem "apple"
List1.AddItem "banana"
List1.AddItem "grape"
List1.AddItem "orange"
List1.AddItem "strawberry"

dicTemp.Add "apple", "苹果"
dicTemp.Add "orange", "柑橘"
dicTemp.Add "banana", "香蕉"
dicTemp.Add "grape", "葡萄"
dicTemp.Add "strawberry", "草莓"
Text1.Text = ""
End Sub

Private Sub Form_Unload(Cancel As Integer)
dicTemp.RemoveAll
Set dicTemp = Nothing
End Sub

Private Sub List1_Click()
Dim astr As String

astr = List1.List(List1.ListIndex)

Text1.Text = dicTemp.Item(astr)
End Sub

Private Sub List1_DblClick()
Dim astr As String

Text1.Text = ""
astr = List1.List(List1.ListIndex)
List1.RemoveItem (List1.ListIndex)
dicTemp.Remove (astr)
End Sub

运行程序,点击List1中的项目,相对应的中文解释就会出现在Text1中间。
上面的程序十分简单,但是这个字典对象具有很多优点:首先这是一个对象,这就使得程序的可读性和可维护性变的十分好。同时它的操作也十分简单,利用AddItem方法就可以增加一个具有关键字的项目,利用Item属性就可以返回一个关键字相对应的项目,利用Remove方法就可以删除一个项目。

摘自:网络整理

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值