Read Data From A Text File Using VB.NET


The example below shows how you can open a textfile ( .txt) and read the data by using VB.NET. Assuming the text file "contact.txt" stored in C:\ drive contains the following 3 lines (Name, Contact Number and Address)


----------------------contact.txt---------------------
Kimberly Wood
570-897-7676227
Main Avenue PO Box 987, Palmerton, PA 18071
------------------------------------------------------


See the example shown below on how to read these data using VB.NET. You might need to include System.IO in your project reference section before you can execute the code.



'-----declare Variables

Dim Name, ContactNo, Address As String

Dim fs as New FileStream("C:\contact.txt", FileMode.Open, FileAccess.Read)
Dim wr as New StreamReader(fs)

wr.BaseStream.Seek(0, SeekOrigin.Begin) '---Go to beginning of file

Name &= wr.ReadLine '--- Read Line 1 data into variable Name
ContactNo &= wr.ReadLine '--- Read Line 1 data into variable ContactNo
Address &= wr.ReadLine '--- Read Line 1 data into variable Address

wr.Close()



At last, the three line of data from the text file is now read and stored into the three variables Name, ContactNo and Address variable in the program.


See Also How To Write Data Into A Text File using VB.NET
See Also How To Read Data From A Text File using VB6 / Classic
See Also How To Write Data Into A Text File using VB6 / Classic

0 comments