Public Class Form1
    Dim path As String = My.Computer.FileSystem.CurrentDirectory & "\contact.txt"

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If My.Computer.FileSystem.FileExists(path) = False Then
            MsgBox("您尚未建立任何聯絡人資料！", MsgBoxStyle.Critical, "檔案無聯絡人資料")
        Else
            Dim reader As New Microsoft.VisualBasic.FileIO.TextFieldParser(path)
            '設定要剖析含有分隔字元的檔案
            reader.TextFieldType = FileIO.FieldType.Delimited
            reader.SetDelimiters(",")   '設定分隔字元
            Dim currentRow As String()
            '當未讀取到檔尾時，要執行While-Wend迴圈內的敘述
            Static i As Short
            While Not reader.EndOfData
                '將剖析一行後的結果(含數個資料欄位)設定給變數currentRow
                currentRow = reader.ReadFields()
                Dim currentField As String
                '使用For Each-Next敘述來依序擷取變數curentRow中資料欄位，並將擷取出
                '來的值設定給變數currentField，接著設定到TextBox5中，以顯示剖析結果
                For Each currentField In currentRow
                    If i = 0 Then
                        TextBox5.Text = "姓名" + vbTab + "電話" + vbTab + vbTab + "e-mail" + vbTab + vbTab + "住址"
                    Else
                        TextBox5.Text += currentField + vbTab   'vbTab為定位字元
                    End If
                Next
                i += 1
                TextBox5.Text += vbCrLf
            End While
            MsgBox("目前共有 " & i - 1 & " 筆聯絡人記錄", , "聯絡人資料筆數")
        End If
        TextBox1.Focus()
    End Sub
End Class
