File.OpenRead Method

Opens an existing file for reading.

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

Public Shared Function OpenRead ( _
	path As String _
) As FileStream
Dim path As String
Dim returnValue As FileStream

returnValue = File.OpenRead(path)
public static FileStream OpenRead (
	String path
public static function OpenRead (
	path : String
) : FileStream
Not applicable.



The file to be opened for reading.

Return Value

A read-only FileStream on the specified path.

Exception typeCondition


path is a zero-length string, contains only white space, or contains one or more invalid characters as defined by InvalidPathChars.


path is a null reference (Nothing in Visual Basic).


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.

This method is equivalent to the FileStream(String,FileMode,FileAccess,FileShare) constructor overload with a FileMode value of Open, a FileAccess value of Read and a FileShare value of Read.

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"
        Dim fs As FileStream

        ' Delete the file if it exists.
        If File.Exists(path) = False Then
            ' Create the file.
            fs = 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 If

        ' Open the stream and read it back.
        fs = File.OpenRead(path)
        Dim b(1024) As Byte
        Dim temp As UTF8Encoding = New UTF8Encoding(True)

        Do While fs.Read(b, 0, b.Length) > 0
    End Sub
End Class

import System.*;
import System.IO.*;
import System.Text.*;

class Test
    public static void main(String[] args)
        String path = "c:\\temp\\MyTest.txt";

        // Delete the file if it exists.
        if (!(File.Exists(path))) {
            // Create the file.
            FileStream fs = File.Create(path);
            try {
                ubyte info[] = (new UTF8Encoding(true)).GetBytes("This is " 
                    + "some text in the file.");

                // Add some information to the file.
                fs.Write(info, 0, info.length);
            finally {

        // Open the stream and read it back.
        FileStream fs = File.OpenRead(path);
        try {
            ubyte b[] = new ubyte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
            while (fs.Read(b, 0, b.length) > 0) {
        finally {
    } //main
} //Test

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0

Community Additions