BaseStream Property
Collapse the table of content
Expand the table of content

StreamReader.BaseStream Property

Returns the underlying stream.

[Visual Basic]
Public Overridable ReadOnly Property BaseStream As Stream
[C#]
public virtual Stream BaseStream {get;}
[C++]
public: __property virtual Stream* get_BaseStream();
[JScript]
public function get BaseStream() : Stream;

Property Value

The underlying stream.

Remarks

StreamReader might buffer input such that the position of the underlying stream will not match the StreamReader position. The StreamReader constructors with the detectEncodingFromByteOrderMarks parameter can change the encoding the first time you read from the StreamReader.

For an example of using this property, 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

File.AppendText

FileInfo.AppendText

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

Example

[Visual Basic, C#, C++] This example shows a use of BaseStream with Seek and SeekOrigin to set the file pointer of the underlying stream to the beginning.

[Visual Basic] 
Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        Dim fs As New FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Read)
        ' Create a character reader.
        Dim w As New StreamReader(fs)
        ' Set the StreamReader file pointer to the end.
        w.BaseStream.Seek(0, SeekOrigin.End)
    End Sub
End Class

[C#] 
using System;
using System.IO;

public class Test
{
    public static void Main()
    {
        FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate,
            FileAccess.Read);
        // Create a character reader.
        StreamReader w = new StreamReader(fs);
        // Set the StreamReader file pointer to the end.
        w.BaseStream.Seek(0, SeekOrigin.End);
    }
}

[C++] 
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;

int main()
{
    FileStream* fs = new FileStream(S"log.txt", FileMode::OpenOrCreate,
        FileAccess::Read);
    // Create a character reader.
    StreamReader* w = new StreamReader(fs);
    // Set the StreamReader file pointer to the end.
    w->BaseStream->Seek(0, SeekOrigin::End);
}

[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.

Requirements

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 | Working with I/O | Reading Text from a File | Writing Text to a File

Show:
© 2016 Microsoft