Opens an existing file for reading.
Assembly: mscorlib (in mscorlib.dll)
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).
path specified a directory.
The caller does not have the required permission.
The file specified in path was not found.
path is in an invalid format.
An I/O error occurred while opening the file.
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 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 fs As FileStream = File.OpenRead(path) Dim b(1024) As Byte Dim temp As UTF8Encoding = New UTF8Encoding(True) Do While fs.Read(b, 0, b.Length) > 0 Console.WriteLine(temp.GetString(b)) Loop End Using End Sub End Class