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
下面的范例介绍了如何建立一个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
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
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
接下来要介绍的是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方法就可以删除一个项目。