This documentation is archived and is not being maintained.

FileSystem.OpenTextFileReader Method (String, Encoding)

Opens a StreamReader object to read from a file.

Namespace:  Microsoft.VisualBasic.FileIO
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)

public static StreamReader OpenTextFileReader(
	string file,
	Encoding encoding
)

Parameters

file
Type: System.String
File to be read.
encoding
Type: System.Text.Encoding
The encoding to use for the file contents. Default is ASCII.

Return Value

Type: System.IO.StreamReader
StreamReader object to read from the file

ExceptionCondition
ArgumentException

The file name ends with a backslash (\).

FileNotFoundException

The specified file cannot be found.

SecurityException

The user lacks necessary permissions to read from the file.

Only text files can be read with a StreamReader.

The following table lists examples of tasks involving the My.Computer.FileSystem.OpenTextFileReader method.

This example opens the file Testfile.txt, reads a line from it, and displays the line in a MessageBox.


Dim fileReader =
  My.Computer.FileSystem.OpenTextFileReader("C:\testfile.txt")
Dim stringReader = fileReader.ReadLine()
MsgBox("The first line of the file is " & stringReader)


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Show: