+92 332 4229 857 99ProjectIdeas@Gmail.com

How to read a file (Vb.net)

How to read from a file
The namespace used for reading a file is System.IO which contains built-in classes and functions to handle files.
Seek is used to move cursor to different locations on file for example SeekOrigin function has three different positions to seek:
Peek Returns next available character

Source code for reading a file
Dim fs As FileStream
fs = New FileStream("newWordFile.docx.doc", FileMode.Open, FileAccess.Read)
Dim sr As StreamReader
sr = New StreamReader(fs)
sr.BaseStream.Seek(0, SeekOrigin.Begin)
  While sr.Peek() > -1
   TextBox1.Text = TextBox1.Text & sr.ReadLine()
  End While
First of all an object of FileStream class is declared, which is used for reading and writing operations and a file named newWordFile.docx is opened for reading. After that an object of StreamWriter class is declared and an object of FileStream is passed as an argument to StreamWriter which further reads a file. At the end the StreamWriter object is closed because the file is read.

Related articles