Reading and writing files in VB.NET

Some years ago, I developed a word processor application in VB.NET that I myself used for some time as a replacement to the popular notepad. The most important part of the application was developing an ability to read and write files from the hard disk. After a lot of researching, I found out two classes buried in the System.IO namespace that do just that thing. And they are called StreamReader and StreamWriter. So in this post I’m going to give a short tutorial on how to read and write files to the hard disk.

Below is a snippet that can be used to read any file (as plain text) from the disk.

First, the reading part.

Sub ReadSomeFile()
  Dim reader As New System.IO.StreamReader("D:/file.txt")
  Dim inputString As String = reader.ReadToEnd()
  reader.Close()
  TextBox1.Text = inputString
End Sub

The first line i.e. Dim reader ... declares and initializes a new instance of the StreamReader class and provides it the full path of the file (with the extension). In simple words, an object is ready to work for us. The second line declares the inputString that is going to hold the input for us. The ReadToEnd() is a method that reads everything from the start of the file to the end of the file. In short, the whole file. The third line just closes the connection between the reader class and file. Why this is needed is because if you try to change, copy or move the file during the reading process, Windows will give you an error saying that the file is in use by some other program. So when the work is done, it is better to close the file. The last line only shows the content into a textbox. That’s it.

Now onto the writing part.

Sub WriteSomeFile()
  Dim writer As New System.IO.StreamWriter("D:/newFile.txt")
  Dim outputString As String = "Hello World! This text is going to be written..."
  writer.Write(outputString)
  writer.Close()
End Sub

The first line above initializes a new StreamWriter and provides the full path of the new file. You can even overwrite existing files. The second line declares and initializes the string that is to be written to the new file. The third line does the actual work. The Write() is a method that writes everything that is provided to the file. Here in this case, I have provided the outputString. The last line is just to close the file and complete the operation.

You may change these functions as per your requirement. Like for example, you may dynamically assign the file name by taking input from the user or you can write the contents of a text box to a file. You can read or write anything once you have understood the working of these two classes. And yes, this method can not only read .txt files but can also read any other file type. The only thing is that it will read and write everything in plain text.


So this was my super short tutorial on reading and writing files in VB.NET using StreamReader and StreamWriter classes. I hope that helped you. Thanks for reading. If you have any questions, you may ask them in the comments section below. Peace.

Advertisements

2 thoughts on “Reading and writing files in VB.NET

  1. Is there anyway to write the file without using Streamwriter and have it NOT overwrite the current file? I am using DOS commands with the >> C:\FileName.txt but it overwrites the file I want it to append the file but I don’t want to use StreamWriter.

    Like

Your Thoughts -

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s