Files and Folders

Reading and writing to files

Reading and writing files is one of the first things you will want to know how to do in Visual Basic, so here's how!

When you read and write files from Visual Basic, you always follow a number of steps:
1) Get a free file number using the FreeFile function. This is used to identify the file once you have opened the file.
2) Use the Open function to open the file for Input (read) or Output (write).
3) Perform an action on the file, either reading it or writing to it.
4) Close the file

To get a free file number, you use the FreeFile function:

' Get a free file number
nFileNum = FreeFile

Then, to open the file, we use the Open function. This takes the following syntax:

Open Filename For Method As FileNumber

Filename is the name of the file, Method is Input or Output (there are other methods, and these are discussed later), and FileNumber is a free file number. For example, the following code opens test.txt for Input:

' Get a free file number
nFileNum = FreeFile
' Open Test.txt for input. App.Path returns the path your app is saved in
Open App.Path & "\test.txt" For Input As nFileNum

If you aren't sure when to use Input and when to use Output, or, like me, can never remember which way round they go, take a look at the diagram below:

Next, we need to do something with it! To get some text from the file, we use the Input statement:

Input(Length, FileNumber)

So, if we wanted to load the first 100 characters from the file, we would use

Input(100, nFileNum)

where nFileNum is the file number we opened the file with.

You can then call the function again to read the next 100 characters and so on. If you want to move where you currently are in the file, use the Seek function:

Seek(FileNumber, Position)

For example, the following code loads the characters from 0 to 10, from 10 to 20, and then from 30 to 40:

sText = Input(10, nFileNum) 'first 10 chars
sText = sText & Input(10, nFileNum) 'next 10 chars
Seek nFileNum, 30 'go to char 30
sText = sText & Input(10, nFileNum) 'chars 30-40

If you want to get the whole file, you can use the LOF (Length of File) function. The following line returns the whole file:

Input(LOF(nFileNum), nFileNum)

If, on the other hand you want to write to the file, you use the Write function:

Write #nFileNum, Text

where, as usual nFileNum is the file number we opened the file with, and Text is the text we want to write to it. Please note that any text you write to the file will overwrite any existing text. If you don't want to do this, take a look at the Appending To Files section. Also note that each time you call the Write function, VB will automattically add a new line. So, if you write:

Write #nFileNum, "hello"
Write #nFileNum, "hello again"

you will get

hello
hello again

instead of

hellohello again

For those of you who are inquistive, and asking why we need the # before the file number, the answer is I don't know!

When you have finished with the file, you use the Close statement to close the file. VB does actually do this for you when your application closes, but it is a good habit to get into.

Close(nFileNum)

To put this all together, take a look at the following code. This reads the whole contents of Test.txt into TextBox1.

Dim nFileNum As Integer

' Get a free file number
nFileNum = FreeFile

' Open Test.txt for input. App.Path returns the path your app is saved in
Open App.Path & "\Test.txt" For Input As nFileNum

' Read the contents of the file into TextBox1
TextBox1.Text = Input(LOF(nFilenum), nFileNum)

' Close the file
Close nFileNum

and, to write the contents of TextBox1 back to the file, you use the following code:

Dim nFileNum As Integer

' Get a free file number
nFileNum = FreeFile

' Create Test.txt
Open App.Path & "\test.txt" For Output As nFileNum

' Write the contents of TextBox1 to Test.txt
Write #nFileNum, TextBox1.Text

' Close the file
Close nFileNum

Simple - Now, if you want to do some more advanced file reading/writing functions, go to the next page!

You might also like...

Comments

About the author

James Crowley

James Crowley United Kingdom

James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audien...

Interested in writing for us? Find out more.

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Hofstadter's Law: It always takes longer than you expect, even when you take into account Hofstadter's Law.”