Visual Basic 程式設計 檔案存取
檔案控制項元件
File System Objects
2
DriveList
屬性
Drive 目前磁碟機代碼
List 可選擇磁碟機
ListCount 可選擇磁碟機個數
ListIndex 已選擇的磁碟機的位置
事件
Change 所選擇的磁碟機改變時
3
DriveListBox (cont.)
Private Sub Drive1_Change() Print Drive1.Drive
Print Drive1.List(Drive1.ListIndex) Print Drive1.ListCount
Print Drive1.ListIndex End Sub
Private Sub form_click() Drive1.Drive = "c:"
End Sub
4
DirListBox
屬性
Path 目前目錄 List 可選擇目錄
ListCount 目前目錄下可選擇目錄個數 ListIndex 已選擇目錄的位置
事件
Change 所選擇的目錄改變時
5
Private Sub Dir1_Change() MsgBox (Dir1.Path)
End Sub
Private Sub form_click() Cls
Print Dir1.Path
Print Dir1.ListCount
Print Dir1.List(Dir1.ListIndex) Print Dir1.List(0)
End Sub
6
FileListBox
屬性
FileName 已選擇檔案的檔名
Path 目前目錄
List 可選擇檔案
ListCount 目前目錄下可選擇檔案數目 ListIndex 已選擇檔案的位置
MultiSelect 可否重複選擇
Selected(數字) 某個元素是否有被選
Pattern 要顯示的檔案型態(過濾方式),
如:*.* 或 *.txt ……
7
FileListBox (cont.)
事件 Click
PatternChange
PathChange 目前目錄改變時
8
FileListBox (cont.)
Private Sub File1_Click() Print File1.FileName
Print File1.Path
Print File1.Path + "\" + File1.FileName End Sub
Private Sub Form_Activate() File1.Path = "c:\temp"
End Sub
9
整合
FileListBox
DirListBox DriveList
10
整合 (cont.)
Private Sub updatePath()
Text1.Text = Dir1.Path + File1.FileName End Sub
Private Sub Command1_Click() File1.Pattern = Text2.Text End Sub
Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub
11
整合 (cont.)
Private Sub Dir1_change() File1.Path = Dir1.Path updatePath
End Sub
Private Sub File1_click() updatePath
End Sub
Visual Basic 程式設計 檔案存取
檔案控制項元件
File System Objects
13
File System Objects
14
File System Objects (cont.)
File System Objects共有五種物件
FileSystemObject
File
Folder
Drive
TextStream
15
FileSystemObject (cont.)
屬性
Drives 傳回目前本機上的磁碟機的 collection
方法
CopyFile 複製檔案 CopyFolder 複製目錄 CreateFolder 產生新檔案 CreateTextFile 產生新文字檔 DeleteFile 刪除檔案
16
FileSystemObject (cont.)
方法
DeleteFolder 刪除目錄
DriveExists 回傳磁碟機是否存在 FileExists 回傳檔案是否存在 FolderExists 回傳目錄是否存在 GetAbsolutePathName 取得絕對路徑名 GetDrive 取得Drive物件 GetDriveName 取得磁碟機名 GetFile 取得File物件
17
FileSystemObject (cont.)
方法
GetFileName 取得檔案名
GetFolder 取得Folder物件 GetParentFolderName 取得父目錄名 GetTempName 取得暫存檔檔名
MoveFile 搬移檔案
MoveFolder 搬移目錄 OpenTextFile 開啟文字檔
18
FileSystemObject (cont.)
產生新的FileSystemObject
Dim 變數名 As New FileSystemObject
Drive, Folder, File, TextStream亦同
CopyFile 來源, 目的[, 覆蓋模式]
來源、目的: 字串
覆蓋模式: boolean,預設值為true
來源、目的可用*與?
19
FileSystemObject (cont.)
Private Sub form_click()
Dim fs As New FileSystemObject
fs.CopyFile "c:\autoexec.bat", "c:\test", _ False
End Sub
20
FileSystemObject (cont.)
CopyFolder 來源, 目的[, 覆蓋模式]
將來源目錄下所有的檔案、目錄,複製到目 的目錄
CopyFolder “c:\temp”, “c:\tmp”
將來源目錄下某些目錄複製到目的目錄
Copy Folder “c:\temp\a*”, “c:\tmp”
21
FileSystemObject (cont.)
CreateFolder 目錄名
產生新目錄
若目錄已存在,則發生錯誤
CreateTextFile 檔名[, 覆蓋模式]
產生新檔案
傳回TextStream
Private Sub form_click()
Dim fs As New FileSystemObject fs.CreateFolder "c:\a"
fs.CreateTextFile "c:\a\test"
End Sub
22
FileSystemObject (cont.)
DeleteFile 檔名[, force]
檔名可包含*與?
若force=false, 則無法刪除屬性是read only 的檔案
DeleteFolder 目錄名[, force]
目錄名可包含*與?
若force=false, 則無法刪除屬性是read only 的目錄
即使目錄內有檔案也會刪除
23
FileSystemObject (cont.)
Private Sub Command1_Click()
Dim fs As New FileSystemObject
fs.DeleteFile File1.Path + "\" + _ File1.FileName
File1.Refresh End Sub
Private Sub Dir1_Change() File1.Path = Dir1.Path End Sub
Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub
24
FileSystemObject (cont.)
DriveExists 磁碟機名
檢查該磁碟機是否存在
DriveExists “c:”
若CDROM沒有光碟片,也會回傳true。要用 Drive物件的IsReady來偵測
FileExists 檔案名
FolderExists 目錄名
25
FileSystemObject (cont.)
GetAbsolutePathName 路徑名
目錄名 回傳值
“c:” “c:\mydocuments\reports”
“c:..” “c:\mydocuments\”
“c:*.*\may97” “c:\mydocuments\reports\*.*\may 97
“region1” “c:\mydocuments\reports\region1
26
FileSystemObject (cont.)
GetDrive 磁碟機名
回傳Drive物件
GetDriveName 磁碟機名 GetFile 檔案名
回傳File物件
GetFileName 路徑名
27
FileSystemObject (cont.)
GetFolder 目錄名
回傳Folder物件
GetParentFolderName 路徑名
GetParentFolderName(“c:\a\b\c”)Γc:\a\b”
GetTempName
MoveFile 來源, 目的
MoveFolder來源, 目的
28
FileSystemObject (cont.)
OpenTextFile 檔名[, IO模式[,create]]
IO模式
ForReading
ForAppending
Create: 若檔案不存在,是否開新檔案
傳回TextStream
29
FileSystemObject (cont.)
30
Private Sub Dir1_Change() File1.Path = Dir1.Path End Sub
Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub
Private Sub File1_Click()
Dim fs As New FileSystemObject
Text1.Text = "GetDriveName: " + _
fs.GetDriveName(File1.Path) _ + vbNewLine + _
"GetParentFolderName: " + _
fs.GetParentFolderName(File1.Path) _ + vbNewLine + _
"GetTempName: " + fs.GetTempName() End Sub
31
Drive
AvailableSpace 可用空間大小(bytes) DriveLetter 磁碟機代碼
DriveType 磁碟機型別 FileSystem 檔案系統
FreeSpace 剩餘空間大小(bytes) IsReady
TotalSize 總體空間大小(bytes)
32
Drive (cont.)
DriveType
0:Unknown(未知)
1:Removable(磁片……)
2:Fixed(硬碟……)
3:Network(網路磁碟機)
4:CD-ROM
5:RAM-DISK
33
Drive (cont.)
Private Sub Drive1_Change()
Dim fs As New FileSystemObject Dim drv As Drive
Set drv = fs.GetDrive( _
Mid(Drive1.Drive, 1, 2)) Cls
If (Not drv.IsReady) Then
MsgBox "Drive is not ready"
Exit Sub End If
Print drv.AvailableSpace; " bytes"
Print drv.DriveLetter
程式還沒完
34
Drive (cont.)
Select Case drv.DriveType Case 0: Print "Unknown"
Case 1: Print "Removable"
Case 2: Print "Fixed"
Case 3: Print "Network"
Case 4: Print "CD-ROM"
Case 5: Print "RAM Disk"
End Select
Print drv.FileSystem
Print drv.FreeSpace; " bytes"
Print drv.IsReady
Print drv.TotalSize; "bytes"
End Sub
35
File
DateCreate 檔案產生日期
DateLastAccessed 最近被讀取/更改日期 DateLastModified 最近被更改日期
Drive 所在磁碟機
Size 檔案大小
Path 路徑名
Name 檔名
方法
OpenAsTextStream 開啟檔案
36
Folder
DateCreate 檔案產生日期
DateLastAccessed 最近被讀取/更改日期 DateLastModified 最近被更改日期
Drive 所在磁碟機
Size 檔案大小
Path 路徑名
Name 檔名
37
File and Folder
38
File and Folder (cont.)
Dim fs As New FileSystemObject Private Sub Drive1_Change()
Dim drv As Drive
Set drv = fs.GetDrive(Drive1.Drive) If (Not drv.IsReady) Then
MsgBox "Drive is not ready!"
Dir1.Enabled = False File1.Enabled = False Exit Sub
End If
Dir1.Enabled = True File1.Enabled = True
Dir1.Path = Drive1.Drive End Sub
程式還沒完
39
Private Sub dir1_change() Dim dir As Folder
File1.Path = Dir1.Path Cls
Set dir = fs.GetFolder(Dir1.Path) Print dir.DateCreated
Print dir.DateLastAccessed Print dir.DateLastModified Print dir.Size
End Sub
Private Sub file1_click() Dim fl As file
Cls
Set fl = fs.GetFile(File1.Path + "\" + _ File1.FileName)
Print fl.DateCreated
Print fl.DateLastAccessed Print fl.DateLastModified Print fl.Size
End Sub
File and Folder (cont.)
40
TextStream (循序檔)
產生方法
[File].OpenAsTextStream
[FileSystemObject].OpenTextFile 屬性
AtEndOfLine 傳回是否在最後一行 AtEndOfStream 傳回是否在檔尾
Column 目前位置
Line 目前在第幾行
41
TextStream (cont.)
方法
Close 關閉檔案
Read 讀取某數量的字元(傳回String) ReadAll 讀取全部內容(傳回String)
ReadLine 讀一行
Skip 跳過某數量的字元
SkipLine 跳過幾行
Write 寫入一個String WriteBlankLines 寫入幾個空白行 WriteLine 寫入一行
42
TextStream (cont.)
Private Sub Command1_Click()
Dim fs As New FileSystemObject Dim st As TextStream
If (Not fs.FileExists(Text1.Text)) Then MsgBox "File does not exist!!"
Exit Sub End If
Set st = fs.OpenTextFile(Text1.Text, _ ForReading)
MsgBox st.Read(2), , "Read"
MsgBox st.ReadLine, , "ReadLine"
MsgBox st.ReadAll, , "ReadAll"
End Sub
43
小小編輯器
44
小小編輯器 (cont.)
Dim fileName As String
Dim fs As New FileSystemObject Private Sub readFile()
Dim st As TextStream
Set st = fs.OpenTextFile(fileName, ForReading) Text1.Text = st.ReadAll()
End Sub
Private Sub writeFile() Dim st As TextStream
fs.DeleteFile (fileName)
Set st = fs.OpenTextFile(fileName, _ ForAppending, True)
st.WriteLine (Text1.Text) End Sub
程式還沒完
45
小小編輯器 (cont.)
Private Sub Command1_Click()
CommonDialog1.Filter = "文字檔(*.txt)|*.txt"
CommonDialog1.ShowOpen
fileName = CommonDialog1.fileName If (fileName = "") Then
Exit Sub End If
readFile
Text1.Enabled = True
Command1.Enabled = False Command2.Enabled = True End Sub
Private Sub Command2_Click() writeFile
Text1.Text = ""
tetx1.Enabled = False Command1.Enabled = True Command2.Enabled = False End Sub