2012-03-04 29 views

回答

3

这是很简单的哥哥:

Private Sub FillRichTextBoxFromFile(ByVal path As String) 
     Try 
      If IO.File.Exists(path) Then 
       Using sr As New IO.StreamReader(path) 
        Dim s As String = "" 
        Dim i As Integer = 1 
        While Not sr.EndOfStream 
         s += CStr(i) + ". " + sr.ReadLine + vbNewLine 
         i += 1 
        End While 
        RichTextBox1.Text = s 
       End Using 
      Else 
       MsgBox("Oooops, File not found !!!") 
      End If 
     Catch ex As Exception 
      MsgBox(ex.Message) 
     End Try 
    End Sub 
1

你也可以试试这个

Dim fileContents() As String 

    If Not My.Computer.FileSystem.FileExists("C:\New.txt") Then 

     MsgBox("File Not Found") 
     Exit Sub 

    End If 

    RichTextBox1.Text = vbNullString 

    fileContents = Split(My.Computer.FileSystem.ReadAllText("C:\New.txt"), vbNewLine) 

    For i = 0 To fileContents.Count - 1 

     RichTextBox1.Text += i + 1 & ". " & fileContents(i) & vbNewLine 

    Next