แก้ไข

แชร์ผ่าน


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.

public:
 static System::IO::StreamReader ^ OpenTextFileReader(System::String ^ file);
public static System.IO.StreamReader OpenTextFileReader (string file);
static member OpenTextFileReader : string -> System.IO.StreamReader
Public Shared Function OpenTextFileReader (file As String) As StreamReader

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.

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.

To See
Open a file with a StreamReader How to: Read Text from Files with a StreamReader (Visual Basic)

See also

Applies to

OpenTextFileReader(String, Encoding)

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

Opens a StreamReader object to read from a file.

public:
 static System::IO::StreamReader ^ OpenTextFileReader(System::String ^ file, System::Text::Encoding ^ encoding);
public static System.IO.StreamReader OpenTextFileReader (string file, System.Text.Encoding encoding);
static member OpenTextFileReader : string * System.Text.Encoding -> System.IO.StreamReader
Public Shared Function OpenTextFileReader (file As String, encoding As Encoding) As StreamReader

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.

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.

To See
Open a file with a StreamReader How to: Read Text from Files with a StreamReader (Visual Basic)

See also

Applies to