290 likes | 402 Views
Visual Basic 程式設計. 講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所. 第十二章 檔案存取. Drive. Drive(cont’d). DriveType 0:Unknown( 未知) 1: Removable( 磁片……) 2: Fixed( 硬碟……) 3: Network( 網路磁碟機) 4: CD-ROM 5:RAM-DISK. Drive(cont’d). 程式還沒完. Private Sub Drive1_Change()
E N D
Visual Basic 程式設計 講師:戴志華 hana@arbor.ee.ntu.edu.tw 國立台灣大學電機工程研究所
Drive(cont’d) • DriveType • 0:Unknown(未知) • 1:Removable(磁片……) • 2:Fixed(硬碟……) • 3:Network(網路磁碟機) • 4:CD-ROM • 5:RAM-DISK
Drive(cont’d) 程式還沒完 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
Drive(cont’d) 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
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
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
TextStream(循序檔) • 產生方法 • [File].OpenAsTextStream • [FileSystemObject].OpenTextFile
TextStream(cont’d) 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
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
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
隨機存取 • 使用者自定型別 Private Type UserRecord Name As String *10 Age As Integer Id As Integer End Type Dim rec As UserRecord rec.Name=“jlhuang” rec.Age=24 rec.Id=11223344 字串長度要固定
開啟檔案 • Open 路徑名(含檔名) For 操作模式 [Access 存取模式]As [#]檔案代號 [Len=單位記錄大小] • Open “c:\test” For Input As #1 Len=10 • 存取模式 • Read • Write • ReadWrite
開啟檔案(cont’d) • 操作模式 • Append • Binary • Input—若檔案不在會產生錯誤 • Output—若檔案存在則覆蓋之 • Random • 關閉檔案 • Close #檔案代號 循 序 操 作 Len=1
開啟檔案(cont’d) 檔案代號已開啟檔案的ID 不能重複使用 FreeFile [range] Dim x as Integer x=FreeFile(0) ‘1 < x < 255 x=FreeFile(1) ‘256 < x < 511
開啟檔案(cont’d) • Close [filenumberlist] Close #File1, #File2, #File3 Close ‘關閉所有開啟中檔案
寫入檔案(隨機存取) • Put [#]檔案代碼,[位置],變數名 • Put #1, 10,MyAge 由1開始數 預設值為1 每寫一次加1 Private Type UserRecord Name As String * 10 Age As Integer Id As Integer End Type Private Sub form_click() Dim rec As UserRecord Dim i As Integer rec.Name = "jlhuang" rec.Age = 24 程式還沒完
寫入檔案(隨機存取) rec.Id = 1122 Print LenB(rec) Open "c:\test" For Random Access Write _ As #1 Len = LenB(rec) For i = 1 To 10 Put #1, , rec rec.Age = rec.Age + 1 rec.Id = rec.Id + 1 Next i Close #1 End Sub
讀取檔案(隨機存取) • Get [#]檔案代碼,[位置],變數名 • Get #1, 10,MyAge • Seek [#]檔案代碼,位置 • Seek #1, 20
讀取檔案(隨機存取) Private Sub form_click() Cls Dim rec As UserRecord Dim i As Integer Open "c:\test" For Random Access Read _ As #1 Len = LenB(rec) Do Get #1, , rec Print rec.Id, rec.Name, rec.Age Loop Until EOF(1) Close #1 End Sub 若回傳true,表示到檔尾
寫入檔案(循序存取) • Print [#]檔案代碼, [output list] Print #1, “Hello World” Print #1, “Hello”; “World”
讀取檔案(循序存取) • Line Input [#]檔案代碼, varname • 以行為單位讀入資料 Do while Not EOF(1) Line Input #1, aline text1.text=text1.text & aline loop
讀取檔案(循序存取) • Input (number, #檔案代碼) • 以字元數為單位讀取資料 Dim x as String x=Input(1, #1)