Opens an existing UTF-8 encoded text file for reading.
Assembly: mscorlib (in mscorlib.dll)
The caller does not have the required permission.
path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.
path is Nothing.
The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.
The specified path is invalid, (for example, it is on an unmapped drive).
The file specified in path was not found.
path is in an invalid format.
This method is equivalent to the StreamReader(String) constructor overload.
The path parameter is permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see GetCurrentDirectory.
For a list of common I/O tasks, see Common I/O Tasks.
The following example opens a text file for reading.
Imports System Imports System.IO Imports System.Text Public Class Test Public Shared Sub Main() Dim path As String = "c:\temp\MyTest.txt" If Not File.Exists(path) Then ' Create the file. Using fs As FileStream = File.Create(path) Dim info As Byte() = _ New UTF8Encoding(True).GetBytes("This is some text in the file.") ' Add some information to the file. fs.Write(info, 0, info.Length) End Using End If ' Open the stream and read it back. Using sr As StreamReader = File.OpenText(path) Do While sr.Peek() >= 0 Console.WriteLine(sr.ReadLine()) Loop End Using End Sub End Class