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