Hi all,

I would like to be able to read data from a text file, be able to edit it and save the new edits to that file over writing the old ones.

Dim FILE_NAME As String = "C:\cat4\text.txt"
        Dim TextLine As String

        If System.IO.File.Exists(FILE_NAME) = True Then

            Dim objReader As New System.IO.StreamReader(FILE_NAME)

            Do While objReader.Peek() <> -1
                TextLine = TextLine & objReader.ReadLine() & vbNewLine
            Loop

            RichTextBox1.Text = TextLine

        Else

            MsgBox("File Does Not Exist")

        End If

    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim mydoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        RichTextBox1.SaveFile(mydoclocation & "c:\cat4\text.txt", RichTextBoxStreamType.PlainText)
    End Sub

You have problem with saving/loading text files, right?

I tested your save-code:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    '
    Dim mydoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments

    Dim TempPath As String
    TempPath = mydoclocation & "c:\cat4\text.txt"
    Debug.Print(TempPath)
    'RichTextBox1.SaveFile(TempPath, RichTextBoxStreamType.PlainText)

End Sub

and the result was "C:\Documents and Settings\<user name>\My Documentsc:\cat4\text.txt" which obviously is not a valid file name, so the fixed code is

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    '
    Dim mydoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments

  RichTextBox1.SaveFile(mydoclocation & "\text.txt", RichTextBoxStreamType.PlainText)

End Sub

and when you read the file

Dim FILE_NAME As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
FILE_NAME = FILE_NAME & "\text.txt"

You may also want to check what else exists in the My-namespace. For example, If My.Computer.FileSystem.FileExists(FILE_NAME) Then and TextLine = My.Computer.FileSystem.ReadAllText(FILE_NAME, System.Text.Encoding.Default) if you want to read a text file with a single line of code.

right i have it working now,

but how do i read say the first line in a text file and put it into a richtextbox1, then read the second line and put it into richtextbox2

wow smelf1 you're just full of questions lately. :)

Ha ha yep, once i get going its hard to stop.

I have the text file reading into a richtextbox and saving back if i edit it.

Private Const _FILE_NAME As String = "C:\cat4\bannedwebsites.txt"
    Private Const _FILE_NAME2 As String = "C:\cat4\bannedprograms.txt"
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BannedWebsite.Click
        Savedata.Show()
        RichTextBox1.Show()
        Information.Text = ("Please enter each seperate website on a new line")
        If My.Computer.FileSystem.FileExists(_FILE_NAME) = True Then

            RichTextBox1.Text = My.Computer.FileSystem.ReadAllText(_FILE_NAME, System.Text.Encoding.ASCII)
        Else : MessageBox.Show("File Does Not Exist")
        End If
    End Sub
    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Savedata.Click
        Dim sMyDoclocation As String = My.Computer.FileSystem.SpecialDirectories.MyDocuments
        RichTextBox1.SaveFile(_FILE_NAME, RichTextBoxStreamType.PlainText)
    End Sub

how do i put line 1 from a text file into richtextbox1 and line 2 from that text file into richtextbox2.

Then if i edit either i can then save and overwrite the original

In that case I would read the file with System.IO.StreamReader

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
  '
  Dim TempPath As String
  Dim OneLine As String
  Dim objReader As System.IO.StreamReader
  Dim Lines As Integer

  TempPath = "D:\temp\foo.txt"
  Lines = 1

  If My.Computer.FileSystem.FileExists(TempPath) Then
    objReader = New System.IO.StreamReader(TempPath)
    Do Until objReader.EndOfStream
      OneLine = objReader.ReadLine
      If (Lines Mod 2) = 0 Then
        ' Even lines
        RichTextBox2.AppendText(OneLine)
        RichTextBox2.AppendText(Environment.NewLine)
      Else
        ' Odd lines
        RichTextBox1.AppendText(OneLine)
        RichTextBox1.AppendText(Environment.NewLine)
      End If
      Lines += 1
    Loop
    objReader.Close()
    objReader = Nothing
  End If

  ' Write two richtextboxes to same file
  Dim AllText As String = ""
  Dim Lines1 As Integer
  Dim Lines2 As Integer
  Lines1 = 0
  Lines2 = 0
  Do Until Lines1 > RichTextBox1.Lines.GetUpperBound(0) AndAlso Lines2 > RichTextBox2.Lines.GetUpperBound(0)
    If Lines1 <= RichTextBox1.Lines.GetUpperBound(0) Then
      AllText = AllText & RichTextBox1.Lines(Lines1) & Environment.NewLine
      Lines1 += 1
    End If
    If Lines2 <= RichTextBox1.Lines.GetUpperBound(0) Then
      AllText = AllText & RichTextBox2.Lines(Lines2) & Environment.NewLine
      Lines2 += 1
    End If
  Loop
  My.Computer.FileSystem.WriteAllText(TempPath, AllText, False)

End Sub

Notice that the above code also merges and writes richtextboxes back to the text file, so you'll have to cut and paste that part to a separate procedure.

You may also try My.Computer.FileSystem.ReadAllText to read the text file and then use Split method to get an array of lines.

This article has been dead for over six months. Start a new discussion instead.