Dela via


My.Computer.FileSystem.OpenTextFileReader Method

Opens a StreamReader.

' Usage
Dim value As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(file)
Dim value As System.IO.StreamReader = My.Computer.FileSystem.OpenTextFileReader(file ,encoding)
' Declaration
Public Function OpenTextFileReader( _
   ByVal file As String _
) As System.IO.StreamReader
' -or-
Public Function OpenTextFileReader( _
   ByVal file As String, _
   ByVal encoding As System.Text.Encoding _
) As System.IO.StreamReader

Parameters

  • file
    String. File to be read. Required.

  • encoding
    Encoding. Default is UTF8. Required.

Return Value

StreamReader

Exceptions

The following conditions may create an exception:

Remarks

Only text files can be read with a StreamReader.

Tasks

The following table lists examples of tasks 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)

Example

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

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

Requirements

Namespace:Microsoft.VisualBasic.MyServices

Class:FileSystemProxy (provides access to FileSystem)

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type

Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

Permissions

The following permission may be necessary:

Permission

Description

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

For more information, see Code Access Security and Requesting Permissions.

See Also

Tasks

How to: Read Text from Files with a StreamReader (Visual Basic)

Reference

My.Computer.FileSystem Object

System.Text.Encoding

System.IO.StreamReader

FileSystem.OpenTextFileReader

Other Resources

Reading from Files in Visual Basic