1 / 36

Visual Basic 程式設計

Visual Basic 程式設計. 講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所. 第十一章 檔案存取. DriveList. DriveListBox(cont’d). DriveListBox(cont’d). Private Sub Drive1_Change() Print " 磁碟代碼=" & Drive1.Drive Print "listcount=" & Drive1.ListCount Print "listindex=" & Drive1.ListIndex

Télécharger la présentation

Visual Basic 程式設計

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Visual Basic 程式設計 講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所

  2. 第十一章 檔案存取

  3. DriveList

  4. DriveListBox(cont’d)

  5. DriveListBox(cont’d) Private Sub Drive1_Change() Print "磁碟代碼=" & Drive1.Drive Print "listcount=" & Drive1.ListCount Print "listindex=" & Drive1.ListIndex Print "目前磁碟所在=" & Drive1.List(Drive1.ListIndex) End Sub Private Sub form_click() Drive1.Drive = "c:" End Sub

  6. DirListBox

  7. DirListBox (cont’d)

  8. DirListBox (cont’d) Private Sub Dir1_Change() MsgBox "目前目錄所在:" & Dir1.Path End Sub Private Sub form_click() Cls Print "目前目錄:" & Dir1.Path Print "目前目錄下可選擇目錄個數:" & Dir1.ListCount Print "已選擇目錄位置:" & Dir1.ListIndex Print "已選擇目錄:" & Dir1.List(Dir1.ListIndex) Print "位置0的目錄:" & Dir1.List(0) End Sub

  9. FileListBox

  10. FileListBox(cont’d)

  11. FileListBox(cont’d)

  12. FileListBox(cont’d) Private Sub File1_Click() Form1.Cls Print "目前目錄: " & File1.Path Print "已選擇檔案的檔名: " & File1.FileName Print "目前目錄下可選擇檔案數目:" & File1.ListCount Print "已選擇檔案的位置: " & File1.ListIndex Print "可否重複選擇: " & File1.MultiSelect Print "某個檔案是否有被選: " & File1.Selected(0) Print "pattern(*.* 或 *.txt): " & File1.Pattern Print "已選擇檔案的完整路徑: "; File1.Path + "\" + File1.FileName End Sub Private SubForm_Activate() File1.Path = "c:\temp" End Sub

  13. Exercise:整合

  14. Exercise:整合(cont’d) 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

  15. Exercise:整合(cont’d) Private Sub Dir1_change() File1.Path = Dir1.Path updatePath End Sub Private Sub File1_click() updatePath End Sub

  16. File System Objects • 專案->設定使用參考元件

  17. File System Objects • File System Objects共有五種物件 FileSystemObject File Folder Drive TextStream

  18. FileSystemObject

  19. FileSystemObject(cont’d)

  20. FileSystemObject(cont’d)

  21. FileSystemObject(cont’d) • 產生新的FileSystemObject 語法: Dim 變數名 AsNew FileSystemObject (*Drive, Folder, File, TextStream亦同) • copyFile 語法: CopyFile來源, 目的 [, 覆蓋模式] • 功能: 複製檔案 • 參數說明: • 來源、目的 : 字串 (來源、目的可用*與?) • 覆蓋模式: boolean,預設值為true

  22. FileSystemObject(cont’d) Private Sub form_click() Dim fs As New FileSystemObject fs.CopyFile "c:\autoexec.bat", "c:\test", False End Sub 執行第二次時,會發生 ERROR!

  23. FileSystemObject(cont’d) • copyFolder 語法: CopyFolder 來源, 目的[, 覆蓋模式] • 功能: 複製目錄 • 範例1: 將來源目錄下所有的檔案、目錄,複製到目的目錄 CopyFolder “c:\temp”, “c:\tmp”,false • 範例2: 將來源目錄下某些目錄複製到目的目錄 Copy Folder “c:\temp\a*”, “c:\tmp”,false

  24. FileSystemObject(cont’d) • createFolder 語法: CreateFolder 目錄名 • 功能: 產生新目錄, 若目錄已存在,則發生錯誤 • createTextFile 語法: CreateTextFile 檔名[, 覆蓋模式] • 功能:產生新檔案 • 說明:傳回TextStream

  25. FileSystemObject(cont’d) Private Sub form_click() Dim fs As New FileSystemObject fs.CreateFolder "c:\a" fs.CreateTextFile "c:\a\test" End Sub

  26. FileSystemObject(cont’d) • DeleteFile 語法: DeleteFile 檔名[, force] • 功能:刪除檔案 • 說明: 檔名可包含*與? • 若force=false, 則無法刪除屬性是read only的檔案

  27. FileSystemObject(cont’d) • DeleteFolder 語法: DeleteFolder 目錄名[, force] • 功能:刪除檔案 • 說明:目錄名可包含*與? • 若force=false, 則無法刪除屬性是read only的目錄 • 即使目錄內有檔案也會刪除

  28. FileSystemObject(cont’d) 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

  29. FileSystemObject(cont’d) • DriveExists 語法: DriveExists 磁碟機名 • 功能: 檢查該磁碟機是否存在 • 說明: 若CDROM沒有光碟片,也會回傳true • 要用Drive物件的IsReady來偵測 • 範例: dim fs as new filesystemobject rlt= fs.DriveExists(“c:”) • FileExists 檔案名 • FolderExists 目錄名

  30. FileSystemObject(cont’d) • GetAbsolutePathName 語法: GetAbsolutePathName pathspec • 說明: 假設當前目錄為 c:\mydocuments\reports

  31. FileSystemObject(cont’d) • GetDrive 語法: GetDrive 磁碟機名 • 說明:回傳Drive物件 • GetDriveName 語法: GetDriveName 磁碟機名 • GetFile 語法: GetFile 檔案名 • 說明:回傳File物件 • GetFileName 語法: GetFileName 路徑名

  32. FileSystemObject(cont’d) • GetFolder 語法: GetFolder 目錄名 • 說明:回傳Folder物件 • GetParentFolderName 語法: GetParentFolderName 路徑名 • 範例: GetParentFolderName(“c:\a\b\c”) “c:\a\b” • GetTempName 語法: GetTempName

  33. FileSystemObject(cont’d) • MoveFile 語法: MoveFile 來源, 目的 • MoveFolder 語法: MoveFolder 來源, 目的

  34. FileSystemObject(cont’d) • OpenTextFile 語法: OpenTextFile 檔名 [, IO模式 [,create] ] • 說明: • IO模式 • ForReading • ForAppending • Create: 若檔案不存在,是否開新檔案(Boolean) • 傳回TextStream

  35. 練習 • show出檔案所在的磁碟名稱 • show出檔案所在的目錄名稱 • show出暫時的檔名

  36. 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

More Related