Home » VB.NetRSS

Using StreamReader to read from a text file

I am using Visual Basic 2010 with Windows 7. My code is is follows:

Dim file as system.IO.StreamReader

file = My.Computer.FileSystem.OpenTextFileReader(C":\financials\program\currency.txt")

TextBox1.text=file.ReadLine()

file.Close

For some reason my program does not execute beyond the Dim statement and instead displays the form without text in TextBox1. I would appreciate your help on this.

Thanks

JFETRAV

 

 

 

 

 

 

 

 

2 Answers Found

 

Answer 1

for one, in your filename you have the " after the C; should be before

and if you use Using, you don't have to close or dispose the StreamReader:

Using sr AsNew System.IO.StreamReader("C:\financials\program\currency.txt")
      TextBox1.Text = sr.ReadLine()
    EndUsing
 

Answer 2

Hi, another example to read  the entire Text File Line by Line. Here into a class property mData

regards Ellen

fnm = OpenFileDialog1.FileName()

      Dim sr As System.IO.StreamReader = New System.IO.StreamReader(fnm)

      DoWhile sr.Peek >= 0
          Me.mData = sr.ReadLine
      Loop
      sr.Close()
 
 
 

<< Previous      Next >>


Microsoft   |   Windows   |   Visual Studio   |   Follow us on Twitter