字體:小 中 大 | |
|
|
2009/03/30 08:13:44瀏覽928|回應34|推薦0 | |
OK! now we had learn about how to use directoryinfo class and now we shall learn about how to read and write a file! use streamwrite class subclass fileinfo subclass createtext and appendtext class to create a file and use append to add text to text file with its content! and then use streamreader and opentext to read file other detail we shall talk about at class! hope you can read the .ppt file first! learn about how to create file and open file and read and add text to file! have fun with it! Public Class Form1 Private Sub cmdOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Label4.Text = "" If tmpfile <> "" Then ListBox1.Items.Add(tmpfile) End If tmpfile = Dir() If tmpfile <> "" Then ListBox1.Items.Add(tmpfile) End If TextBox1.Text = "" End Sub Dim fileNum As Integer Dim counter As Integer = 0 Dim str1 As String Dim filename As String str1 = "" Try fileNum = FreeFile() ' 取得檔案編號 filename = Label1.Text & "\" & TextBox2.Text FileOpen(fileNum, filename, OpenMode.Input) Do Until EOF(fileNum) ' 讀取文字檔案內容 ' 讀取行 str1 &= LineInput(fileNum) & vbNewLine counter += 1 FileClose(fileNum) Label4.Text = "總共 : " & counter & " 行" TextBox1.Text = str1 Catch ex As Exception MsgBox("錯誤: 檔案不存在!") End Try Else MessageBox.Show("請點選檔案 !") Application.Exit() Dim fileNum As Integer Dim counter As Integer = 0 Dim strContent As String = "" Dim filename As String If TextBox2.Text <> "" Then Try fileNum = FreeFile() ' 取得檔案編號 filename = Label1.Text & "\" & TextBox2.Text FileOpen(fileNum, filename, OpenMode.Input) Do Until EOF(fileNum) ' 讀取文字檔案內容 ' 讀取行 strContent &= LineInput(fileNum) & vbNewLine counter += 1 FileClose(fileNum) Label4.Text = "總共 : " & counter & " 行" TextBox1.Text = strContent Catch ex As Exception MsgBox("錯誤: 檔案不存在!") End Try Else MessageBox.Show("請點選檔案 !") Dim fileNum As Integer Dim fileName As String fileName = Label1.Text & "\" & TextBox2.Text fileNum = FreeFile() ' 取得檔案編號 FileOpen(fileNum, fileName, OpenMode.Output) PrintLine(fileNum, TextBox1.Text) ' 寫入 FileClose(fileNum) Else MessageBox.Show("資料不可空白 !") End If End Class next time we shall try to use random file to do the file function! |
|
( 知識學習|其他 ) |