FileSystem.OpenTextFileReader Method

Definition

Opens a StreamReader object to read from a file.

Overloads

OpenTextFileReader(String)

Opens a StreamReader object to read from a file.

OpenTextFileReader(String, Encoding)

Opens a StreamReader object to read from a file.

OpenTextFileReader(String)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

Opens a StreamReader object to read from a file.

C#
public static System.IO.StreamReader OpenTextFileReader(string file);

Parameters

file
String

File to be read.

Returns

StreamReader object to read from the file.

Exceptions

The file name ends with a backslash (\).

The specified file cannot be found.

The user lacks necessary permissions to read from the file.

Examples

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

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

Remarks

Only text files can be read with a StreamReader.

The following table lists an example of a task involving the My.Computer.FileSystem.OpenTextFileReader method.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

OpenTextFileReader(String, Encoding)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

Opens a StreamReader object to read from a file.

C#
public static System.IO.StreamReader OpenTextFileReader(string file, System.Text.Encoding encoding);

Parameters

file
String

File to be read.

encoding
Encoding

The encoding to use for the file contents. Default is ASCII.

Returns

StreamReader object to read from the file.

Exceptions

The file name ends with a backslash (\).

The specified file cannot be found.

The user lacks necessary permissions to read from the file.

Examples

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

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

Remarks

Only text files can be read with a StreamReader.

The following table lists an example of a task involving the My.Computer.FileSystem.OpenTextFileReader method.

See also

Applies to

.NET 10 and other versions
Product Versions
.NET Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1