vb.net 创建与读取文本文件的方法

下面是创建代码:

        If File.Exists("C:\abc.txt") Then
            File.Delete("C:\abc.txt")
        End If
        Dim sr As StreamWriter = File.CreateText("C:\abc.txt")
        sr.Write("晨彦电脑科技")
        sr.WriteLine("WWW.BTOSS.COM")
        sr.Write("这是一个创建文本文件的例子")
        sr.Close()

下面是读取代码:


        If File.Exists("C:\abc.txt") = False Then
            MsgBox("文件不存在!")
        Else
            Dim sr As StreamReader = File.OpenText("C:\abc.txt")
            Dim str As String = sr.ReadLine
            Dim i As Integer = 0
            While Not IsNothing(str)
                i = i + 1
                If i = 1 Then
                    Me.TextBox1.Text &= str
                Else
                    Me.TextBox1.Text &= "," + str
                End If

                str = sr.ReadLine
            End While
        End If

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code