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
整合
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