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

賴孟瑜
作業
2009/06/23 10:07

710079

檔名 710079-42


710067
作業
2009/06/23 10:00

班級作號   40228

姓名   楊芷庭

檔名  h710067-42.exe

學號  710067


710066
710066-9
2009/06/23 09:59

楊小萱

40227

710066

檔名710066-9


40204李柏逸
交作業
2009/06/07 21:08

40204李柏逸

學號710045

檔名h710045.exe


張烜嘉
作業
2009/05/31 20:02

h71016

檔名    546


710056
完成做業
2009/05/26 09:20

檔名  710056.exe

學號  710056

姓名  張廖倖年

班級 402

座號16


胡幸玟
電腦作業
2009/05/26 09:20

學號 710053

班級 2

座號12

檔名710053(7).exe


40214
作業完成
2009/05/05 11:08

班級:402

姓名:唐文正

學號:h710054

檔名:710054-6.exe

夫子(yangfu) 於 2009-05-18 20:24 回覆:
your file had been checked! 

710055
homework
2009/05/05 09:33

班級402

座號15

姓名 張書瑜

檔名71005503.exe

夫子(yangfu) 於 2009-05-18 20:26 回覆:
Checked! your file had been checked!

710073
功課
2009/04/28 09:38

高一二班

劉碩純

學號:710073

檔名:710073003.exe

夫子(yangfu) 於 2009-05-18 20:25 回覆:
your file had been checked!
頁/共 4 頁  回應文第一頁 回應文章上一頁 回應文章下一頁 回應文章最後一頁