This documentation is archived and is not being maintained.

StreamReader Constructor (String)

.NET Framework 1.1

Initializes a new instance of the StreamReader class for the specified file name.

[Visual Basic]
Public Sub New( _
   ByVal path As String _
public StreamReader(
   string path
public: StreamReader(
   String* path
public function StreamReader(
   path : String


The complete file path to be read.


Exception Type Condition
ArgumentException path is an empty string ("").
ArgumentNullException path is a null reference (Nothing in Visual Basic).
FileNotFoundException The file cannot be found.
DirectoryNotFoundException The specified path is invalid, such as being on an unmapped drive.
IOException path includes an incorrect or invalid syntax for file name, directory name, or volume label.


The complete file path is specified by path. The default character encoding and default buffer size are used.

path can be a file name, including a file on a Universal Naming Convention (UNC) share.

path is not required to be a file stored on disk; it can be any part of a system that supports access via streams.

CAUTION   When you compile a set of characters with a particular cultural setting and retrieve those same characters with a different cultural setting, the characters might not be interpretable, and could cause an exception to be thrown.

For an example of using this method, see the Example section below. The following table lists examples of other typical or related I/O tasks.

To do this... See the example in this topic...
Create a text file. Writing Text to a File
Write to a text file. Writing Text to a File
Read from a text file. Reading Text from a File
Append text to a file. Opening and Appending to a Log File



Get the size of a file. FileInfo.Length
Get the attributes of a file. File.GetAttributes
Set the attributes of a file. File.SetAttributes
Determine if a file exists. File.Exists
Read from a binary file. Reading and Writing to a Newly Created Data File
Write to a binary file. Reading and Writing to a Newly Created Data File


[Visual Basic, C#, C++] The following example demonstrates this StreamReader constructor.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test

    Public Shared Sub Main()
        Dim path As String = "c:\temp\MyTest.txt"

            If File.Exists(path) Then
            End If

            Dim sw As StreamWriter = New StreamWriter(path)
            sw.WriteLine("is some text")
            sw.WriteLine("to test")

            Dim sr As StreamReader = New StreamReader(path)

            Do While sr.Peek() >= 0
        Catch e As Exception
            Console.WriteLine("The process failed: {0}", e.ToString())
        End Try
    End Sub
End Class

using System;
using System.IO;

class Test 
    public static void Main() 
        string path = @"c:\temp\MyTest.txt";

            if (File.Exists(path)) 

            using (StreamWriter sw = new StreamWriter(path)) 
                sw.WriteLine("is some text");
                sw.WriteLine("to test");

            using (StreamReader sr = new StreamReader(path)) 
                while (sr.Peek() >= 0) 
        catch (Exception e) 
            Console.WriteLine("The process failed: {0}", e.ToString());

#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";

    try {
        if (File::Exists(path)) {

        StreamWriter* sw = new StreamWriter(path);
        try {
            sw->WriteLine(S"is some text");
            sw->WriteLine(S"to test");
        } __finally {
            if (sw) __try_cast<IDisposable*>(sw)->Dispose();

        StreamReader* sr = new StreamReader(path);
        try {
            while (sr->Peek() >= 0) {
        } __finally {
            if (sr) __try_cast<IDisposable*>(sr)->Dispose();
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

StreamReader Class | StreamReader Members | System.IO Namespace | StreamReader Constructor Overload List | Encoding | Working with I/O | Reading Text from a File | Writing Text to a File