2 Replies - 3265 Views - Last Post: 20 January 2009 - 03:04 PM Rate Topic: -----

#1 shaky120  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-January 09

Trying to get program to read text file and display 2nd line of text

Post icon  Posted 18 January 2009 - 11:31 AM

Hey everyone, save section works fine, but with the load section i can only get it to display all the text or the first line of text of the txt file, im trying to get the program to display the 2nd line of saved text, or 3rd line etc.
:) Thanks..

Shaky

code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonsave.Click
Dim file As System.IO.StreamWriter
file = My.Computer.FileSystem.OpenTextFileWriter("c:\intel\zz.txt", True)
file.WriteLine("Here is the first string." & RichTextBox1.Text)

file.Close()
End Sub

Private Sub Buttonload_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Buttonload.Click
Dim fileReader As System.IO.StreamReader
fileReader = My.Computer.FileSystem.OpenTextFileReader("C:\intel\zz.txt")
Dim stringReader As String
stringReader = fileReader.ReadLine



Do While fileReader.Peek() >= 0
Console.WriteLine(fileReader.ReadLine())
RichTextBox1.Text = fileReader.ReadToEnd
Loop
End Sub

/code

Is This A Good Question/Topic? 0
  • +

Replies To: Trying to get program to read text file and display 2nd line of text

#2 djjeavons  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 19
  • Posts: 89
  • Joined: 09-January 09

Re: Trying to get program to read text file and display 2nd line of text

Posted 20 January 2009 - 05:25 AM

Hi

One option would be to read the entire contents of the file into a string array by splitting the text file based on the carriage return character and then only output the necessary element of the string array. For example:

Dim fileReader As New System.IO.StreamReader("Path to your file")
Dim fileContents() As String = fileReader.ReadToEnd.Split(ControlChars.Cr)

'To display the second line of text use:
MessageBox.Show(fileContents(1))




HTH
Was This Post Helpful? 0
  • +
  • -

#3 shaky120  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 14-January 09

Re: Trying to get program to read text file and display 2nd line of text

Posted 20 January 2009 - 03:04 PM

Hey, thanks alot mate, worked a treat.

Thanks

Shaky
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1