Microsoft KB Archive/315828

= How to read from and write to a text file by using Visual Basic 2005 or Visual Basic .NET =

Article ID: 315828

Article Last Modified on 5/18/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 Q315828





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



SUMMARY
This article demonstrates how to read from and write to a text file by using Microsoft Visual Basic 2005 or Microsoft Visual Basic .NET.

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Visual Basic 2005 or Visual Basic .NET

This article assumes that you are familiar with the following topics:
 * Visual Basic 2005 or Visual Basic .NET

Read and write text files
The &quot;Read a text file&quot; section demonstrates how to use the StreamReader object to read a text file. The &quot;Write a text file (example 1)&quot; section and the &quot;Write a text file (example 2)&quot; section demonstrate how to use the StreamWriter class to write text to a file.

Read a text file
The following code uses the StreamReader class to open, to read, and to close the text file. You can pass the path name of a text file to the StreamReader constructor to open the file automatically. The ReadLine method reads each line of text and increments the file pointer to the next line as it reads. When the ReadLine method reaches the end of the file, it returns Nothing.  Start Microsoft Visual Studio 2005 or Microsoft Visual Studio .NET. Create a new Console Application in Visual Basic 2005 or Visual Basic .NET.  Add the following code to the top of the Module1.vb file: Imports System.IO                     Add the following code to the Sub Main procedure. Dim objStreamReader As StreamReader Dim strLine As String

'Pass the file path and the file name to the StreamReader constructor. objStreamReader = New StreamReader(&quot;C:\Boot.ini&quot;)

'Read the first line of text. strLine = objStreamReader.ReadLine

'Continue to read until you reach the end of the file. Do While Not strLine Is Nothing

'Write the line to the Console window. Console.WriteLine(strLine)

'Read the next line. strLine = objStreamReader.ReadLine Loop

'Close the file. objStreamReader.Close

Console.ReadLine  On the Debug menu, click Start to compile and to run the application. Press ENTER to close the Console window.

The Console window displays the Boot.ini file. Note that the contents of the Boot.ini file may vary from configuration to configuration. The following output is a sample Boot.ini file:

[boot loader] timeout=5 default=multi(0)disk(0)rdisk(0)partition(1)\WINNT

[operating systems] multi(0)disk(0)rdisk(0)partition(1)\WINNT=&quot;Windows 2000 Professional&quot; /fastdetect C:\ = &quot;Windows 98&quot;

</ol>

Write a text file (example 1)
The following code uses the StreamWriter class to open, to write to, and to close the text file. Similar to StreamReader, you can pass the path name of a text file to the StreamWriter constructor to open the file automatically. The WriteLine method writes a complete line of text to the text file. <ol> Start Visual Studio 2005 or Visual Studio .NET.</li> Create a new Console Application in Visual Basic 2005 or Visual Basic .NET.</li>  Add the following code to the top of Module1.vb: Imports System.IO                   </li>  Add the following code to the Sub Main procedure: Dim objStreamWriter As StreamWriter 'Pass the file path and the file name to the StreamWriter constructor. objStreamWriter = New StreamWriter(&quot;C:\Testfile.txt&quot;)

'Write a line of text. objStreamWriter.WriteLine(&quot;Hello World&quot;)

'Write a second line of text. objStreamWriter.WriteLine(&quot;From the StreamWriter class&quot;)

'Close the file. objStreamWriter.Close </li> On the Debug menu, click Start to compile and to run the application.

This code creates a file named Test.txt on drive C. Open Test.txt in a text editor such as Notepad. Test.txt contains two lines of text:

<pre class="fixed_text">Hello World From the StreamWriter class

</li></ol>

Write a text file (example 2)
The following code uses the StreamWriter class to open, to write to, and to close the text file. Unlike the previous example, this code passes two additional parameters to the constructor.

The first parameter is the file path and file name of the file. The second parameter, True, specifies that the file be opened in append mode. If you specify False for the second parameter, the contents of the file are overwritten each time you run the code. The third parameter specifies Unicode so that StreamWriter encodes the file in Unicode. You can also specify the following encoding methods for the third parameter:
 * ASCII
 * BigEndianUnicode
 * UTF7
 * UTF8

The Write method is similar to the WriteLine method except that Write does not automatically embed a carriage return/line feed (CR/LF) character combination. This is useful when you want to write one character at a time. <ol> Start Visual Studio 2005 or Visual Studio .NET.</li> Create a new Console Application in Visual Basic 2005 or Visual Basic .NET.</li>  Add the following code to the top of Module1.vb: Imports System.IO Imports System.Text </li>  Add the following code to the Sub Main procedure under your existing code: Dim objStreamWriter As StreamWriter Dim x As Long 'Open the file. objStreamWriter = New StreamWriter(&quot;C:\Test2.txt&quot;, True, _     Encoding.Unicode)

'Write out the numbers 1 through 10 on the same line. For x = 1 To 10 objStreamWriter.Write(x) Next x  'Close the file. objStreamWriter.Close </li>  On the Debug menu, click Start to compile and to run the application.

This code creates a file that is named Test2.txt on drive C. Open Test2.txt in a text editor such as Notepad. Test2.txt contains a single line of text: <pre class="fixed_text">12345678910 Note If you run the code several times, the &quot;123456789&quot; text is repeated on the same line. </li></ol>

Complete code listing
'Read a Text File Imports System.IO Module Module1

Sub Main Dim objStreamReader As StreamReader Dim strLine As String

'Pass the file path and the file name to the StreamReader constructor. objStreamReader = New StreamReader(&quot;C:\Boot.ini&quot;)

'Read the first line of text. strLine = objStreamReader.ReadLine

'Continue to read until you reach the end of the file. Do While Not strLine Is Nothing

'Write the line to the Console window. Console.WriteLine(strLine)

'Read the next line. strLine = objStreamReader.ReadLine Loop

'Close the file. objStreamReader.Close

Console.ReadLine End Sub End Module

'Write a Text File: Version 1 Imports System.IO Module Module1

Sub Main Dim objStreamWriter As StreamWriter

'Pass the file path and the file name to the StreamWriter constructor. objStreamWriter = New StreamWriter(&quot;C:\Text.txt&quot;)

'Write a line of text. objStreamWriter.WriteLine(&quot;Hello World&quot;)

'Write a second line of text. objStreamWriter.WriteLine(&quot;From the StreamWriter class&quot;)

'Close the file. objStreamWriter.Close

End Sub End Module

'Write a Text File: Version 2 Imports System.IO Imports System.Text Module Module1

Sub Main Dim objStreamWriter As StreamWriter Dim x As Long

'Open the file. objStreamWriter = New StreamWriter(&quot;C:\Test2.txt&quot;, True, _          Encoding.Unicode)

'Write out the numbers 1 through 10 on the same line. For x = 1 To 10 objStreamWriter.Write(x) Next x

'Close the file. objStreamWriter.Close End Sub

End Module

Troubleshooting
Whenever you input or output a file, it is good programming practice to wrap the code inside a Try-End-Try block in case an error occurs. Some possible errors include a file that does not exist, or a file that is already in use.

<div class="references_section">