reading a txt file

how to read a first line of a txt file

Page 1 of 1

1 Replies - 2756 Views - Last Post: 08 December 2008 - 05:44 AM Rate Topic: -----

#1 labe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-December 08

reading a txt file

Post icon  Posted 08 December 2008 - 04:54 AM

Hi i am trying to read a first line of a txt file and display it on a label, but when i display it, it is only displaying the last record of my text file.
here is the code that i have write. any help will be appreciated

Dim readtext As String = ("C:\test.txt")

		If System.IO.File.Exists(readtext) Then

			Dim textfile As New System.IO.StreamReader(readtext)

			Do While textfile.Peek() <> -1

				Label2.Text = textfile.ReadLine
				'TextBox1.Text = textfile.ReadLine
				Label1.Text = textfile.ReadToEnd

			Loop
			textfile.Close()
		Else
			MsgBox("File does not Exist")

		End If



Is This A Good Question/Topic? 0
  • +

Replies To: reading a txt file

#2 koshenoo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 08-December 08

Re: reading a txt file

Posted 08 December 2008 - 05:44 AM

View Postlabe, on 8 Dec, 2008 - 03:54 AM, said:

Hi i am trying to read a first line of a txt file and display it on a label, but when i display it, it is only displaying the last record of my text file.
here is the code that i have write. any help will be appreciated

Dim readtext As String = ("C:\test.txt")

		If System.IO.File.Exists(readtext) Then

			Dim textfile As New System.IO.StreamReader(readtext)

			Do While textfile.Peek() <> -1

				Label2.Text = textfile.ReadLine
				'TextBox1.Text = textfile.ReadLine
				Label1.Text = textfile.ReadToEnd

			Loop
			textfile.Close()
		Else
			MsgBox("File does not Exist")

		End If




Try this code
Dim readText As String = ("C:\test.txt")
Dim textParser As New Textbox
textParser.Multiline = True
textParser.Text = My.Computer.FileSystem.ReadAllText(readText)
MsgBox("The first line is: " & textParser.Lines(0))


I'm not 100% sure that it works... but try it
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1