網路城邦
上一篇 回創作列表 下一篇  字體:
檔案的讀寫
2009/03/30 08:13:44瀏覽927|回應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)

 

   End Sub

 

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

 

       Dim tmpfile As String

 

       ListBox1.Items.Clear()

 

       Label1.Text = CurDir()   '傳回目前的路徑,沒有指定參數是指目前工作磁碟。

        Label4.Text = ""

 

       tmpfile = Dir(Label1.Text & "\*.txt", vbReadOnly)

        If tmpfile <> "" Then

            ListBox1.Items.Add(tmpfile)

        End If

 

        Do Until tmpfile = ""

            tmpfile = Dir()

            If tmpfile <> "" Then

                ListBox1.Items.Add(tmpfile)

            End If

        Loop

 

    End Sub

 

    Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click

        TextBox1.Text = ""

    End Sub

 

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

 

    End Sub

 

    Private Sub cmdRead_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim fileNum As Integer

        Dim counter As Integer = 0

        Dim str1 As String

        Dim filename As String

 

        If TextBox2.Text <> "" Then

            str1 = ""

            Try

                fileNum = FreeFile() ' 取得檔案編號

                filename = Label1.Text & "\" & TextBox2.Text

                FileOpen(fileNum, filename, OpenMode.Input)

                Do Until EOF(fileNum) ' 讀取文字檔案內容

                    ' 讀取行

                    str1 &= LineInput(fileNum) & vbNewLine

                    counter += 1

                Loop

                FileClose(fileNum)

                Label4.Text = "總共 : " & counter & " "

                TextBox1.Text = str1

            Catch ex As Exception

                MsgBox("錯誤: 檔案不存在!")

            End Try

        Else

            MessageBox.Show("請點選檔案 !")

 

        End If

 

    End Sub

 

    Private Sub cmdExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdExit.Click

        Application.Exit()

 

    End Sub

 

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged

        Dim fileNum As Integer

        Dim counter As Integer = 0

        Dim strContent As String = ""

        Dim filename As String

 

        TextBox2.Text = ListBox1.SelectedItem

        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

                Loop

                FileClose(fileNum)

                Label4.Text = "總共 : " & counter & " "

                TextBox1.Text = strContent

            Catch ex As Exception

                MsgBox("錯誤: 檔案不存在!")

            End Try

        Else

            MessageBox.Show("請點選檔案 !")

 

        End If

 

    End Sub

 

    Private Sub cmdWrite_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWrite.Click

        Dim fileNum As Integer

        Dim fileName As String

 

        If TextBox1.Text <> "" And TextBox2.Text <> "" Then

            fileName = Label1.Text & "\" & TextBox2.Text

 

            ' Open File

            fileNum = FreeFile() ' 取得檔案編號

            FileOpen(fileNum, fileName, OpenMode.Output)

            PrintLine(fileNum, TextBox1.Text) ' 寫入

            FileClose(fileNum)

        Else

            MessageBox.Show("資料不可空白 !")

        End If

 

    End Sub

End Class

next time we shall try to use random file to do the file function!

( 知識學習其他 )
回應 推薦文章 列印 加入我的文摘
上一篇 回創作列表 下一篇

引用
引用網址:https://classic-blog.udn.com/article/trackback.jsp?uid=yangfu&aid=2796997

 回應文章 頁/共 4 頁  回應文章第一頁 回應文章上一頁 回應文章下一頁 回應文章最後一頁

123
321
2009/03/31 19:23

40331廖秦利

學號 710110

檔名 WindowsApplication40.exe

夫子(yangfu) 於 2009-04-06 18:23 回覆:
OK! your project file had been checked!

鄧佑任
作業
2009/03/31 12:05

檔名:40439-7.exe

學號:710159

高一四班39號鄧佑任

夫子(yangfu) 於 2009-04-06 18:24 回覆:
Please made a folder call homework and put your assignment work in that folder so that i can access much easy! thanks!

710160
做業
2009/03/31 12:04

檔名:e710160

高一四班40號賴柏志

710160

夫子(yangfu) 於 2009-04-06 18:25 回覆:
OK! your project file had been checked!

710161
作業
2009/03/31 12:02

班級404

學號710161

座號41

夫子(yangfu) 於 2009-04-06 18:26 回覆:
you forget to tell me your filename! it is hw2-7.exe? don't made that mistake again!
頁/共 4 頁  回應文章第一頁 回應文章上一頁 回應文章下一頁 回應文章最後一頁