Microsoft KB Archive/302309

= How to read a text file by using System.IO in Visual Basic 2005 or in Visual Basic .NET =

Article ID: 302309

Article Last Modified on 8/28/2007

-

APPLIES TO


 * Microsoft Visual Basic 2005
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q302309





For a Microsoft Visual C# .NET version of this article, see 306777.



For a Microsoft Visual Basic 6.0 version of this article, see 183638.



SUMMARY
This article demonstrates how to retrieve information from a text file and use an ArrayList class to display that information to the user.

Reading text files in Visual Basic 2005 or in Visual Basic .NET
Opening and reading files for read access is an important part of input/output (IO) functionality, even if you do not need to write to the file in question. This example opens a file for reading, which is useful for text files but is inappropriate for binary files. This example uses one of many methods that are available for opening the file. Although many data structures are available to store information that is retrieved from the file, an ArrayList class is the easiest structure to use. To open and read from the file, this example uses objects from the System.IO namespace, specifically the System.IO.StreamReader class.

Note This sample requires some form of a text (.txt) file from which to read.

To load and read a text file from Microsoft Visual Basic 2005 or from Microsoft Visual Basic .NET, follow these steps:  Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. Create a new Console Application in Visual Basic. Visual Studio creates a Module for you, along with an empty Main procedure.  Make sure that the project references at least the System namespace. Use the Imports statement on the System, System.IO, and System.Collections namespaces so you are not required to qualify declarations from these namespaces later in your code. You must use these statements prior to any other declarations. Imports System Imports System.IO Imports System.Collections   To open a file for reading, create a new instance of a StreamReader object, and pass the file's path into the constructor as follows: Dim objReader As New StreamReader(&quot;c:\test.txt&quot;)   You will need a string variable in which to store each line of the file as you process. Because you will be adding these lines to an ArrayList, declare and create an object of that type as well. Dim sLine As String = &quot;&quot; Dim arrText As New ArrayList   There are several ways to read the file in, including the ReadToEnd method which reads in the entire file at once. However, for this example, you can use the ReadLine method to bring in the file one line at a time. When the end of the file is reached, this method returns &quot;Nothing,&quot; which allows a way to end your loop. As you read each line from the file, you can use the Add method of the ArrayList to insert the lines into your ArrayList class. Do   sLine = objReader.ReadLine If Not sLine Is Nothing Then arrText.Add(sLine) End If Loop Until sLine Is Nothing objReader.Close   Use a &quot;For Each&quot; loop to write the contents of your newly filled ArrayList to the console as follows: For Each sLine In arrText Console.WriteLine(sLine) Next Console.ReadLine </li> Save and run your code, which produces a listing of your file to the console.</li></ol>

Complete code listing
Imports System Imports System.IO Imports System.Collections

Module Module1

Sub Main Dim objReader As New StreamReader(&quot;c:\test.txt&quot;) Dim sLine As String = &quot;&quot; Dim arrText As New ArrayList

Do           sLine = objReader.ReadLine If Not sLine Is Nothing Then arrText.Add(sLine) End If       Loop Until sLine Is Nothing objReader.Close

For Each sLine In arrText Console.WriteLine(sLine) Next Console.ReadLine End Sub

End Module

Pitfalls
There are several things to be aware of when you work with file I/O, including the following items:
 * Any time you access a file, there is the possibility that the file that you are trying to read or write may not be on the system or may be in use.
 * This example reads the entire file into memory before it processes the file. You may encounter a situation in which the file is too large to be held in memory, or you may not have permissions to access the file.

Any of these situations cause an exception to be raised. It is always good practice to provide a try...catch block to handle these common issues.

<div class="references_section">