網路城邦
上一篇 回創作列表 下一篇  字體:
檔案的讀寫
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)

 

   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/04/28 09:31

40233

劉彥琦

71002

檔名 710072~8.exe

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

710071
電腦作業
2009/04/20 23:26

班級:高一二

姓名:廖侰雯

座號:32

學號:710071

檔名:710071(8).exe

夫子(yangfu) 於 2009-05-18 20:28 回覆:
Sorry! but I can't found the file match with your filename you mention in the post article!

林佳潔
作業
2009/04/14 19:19

林佳潔

710048

40207

檔名:h710048  讀寫.exe

夫子(yangfu) 於 2009-05-18 20:30 回覆:
sorry! unable to found the file! are you sure you had put it there?

林文心
功課
2009/04/14 10:04

40206

林文心

檔名:b.exe

710047



等級:
留言加入好友
作業
2009/04/14 10:03

40210洪培馨

檔案:456456.exe

學號:710051


欣蓓
作業
2009/04/14 09:57

402-31

710070

詹欣蓓

h710070-10.exe


40201
HW
2009/04/14 09:50

40201

710042

檔名:hw2.exe


710060
作業
2009/04/14 09:42

陳音如

102  20

學號:710060

檔名:0958.exe


林承寬
做完喏....!(好多)
2009/04/14 09:28

班級:402

姓名:林承寬

座號:08

學號:710049

檔名:h710049_Write & Read.exe


王奕翔
作業
2009/04/14 08:17

班級:401

座號:02

姓名:王奕翔

學號:710002

檔名:H710002-5.exe

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