Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6182903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T01:16:38+00:00 2026-05-24T01:16:38+00:00

I have a textReader that in a specific instance I want to be able

  • 0

I have a textReader that in a specific instance I want to be able to advance to the end of file quickly so other classes that might hold a reference to this object will not be able to call tr.ReadLine() without getting a null.

This is a large file. I cannot use TextReader.ReadToEnd() as it will often lead to an OutOfMemoryException

I thought I would ask the community if there was a way SEEK the stream without using TextReader.ReadToEnd() which returns a string of all data in the file.

Current method, inefficient.
The following example code is a mock up. Obviously I am not opening a file with an if statement directly following it asking if I want to read to the end.

TextReader tr = new StreamReader("Largefile");
if(needToAdvanceToEndOfFile)
{
    while(tr.ReadLine() != null) { }
}

Desired solution (Note this code block contains fake ‘concept’ methods or methods that cannot be used due to risk of outofmemoryexception)

TextReader tr = new StreamReader("Largefile");
if(needToAdvanceToEndOfFile)
{
    tr.SeekToEnd(); // A method that does not return anything. This method does not exist.
    // tr.ReadToEnd() not acceptable as it can lead to OutOfMemoryException error as it is very large file.
}

A possible alternative is to read through the file in bigger chunks using tr.ReadBlock(args).

I poked around ((StreamReader)tr).BaseStream but could not find anything that worked.

As I am new to the community I figured I would see if someone knew the answer off the top of their head.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T01:16:38+00:00Added an answer on May 24, 2026 at 1:16 am

    You have to discard any buffered data if you have read any file content – since data is buffered you might get content even if you seek the underlying stream to the end – working example:

    StreamReader sr = new StreamReader(fileName);
    string sampleLine = sr.ReadLine();
    
    //discard all buffered data and seek to end
    sr.DiscardBufferedData();
    sr.BaseStream.Seek(0, SeekOrigin.End);
    

    The problem as mentioned in the documentation is

    The StreamReader class buffers input from the underlying stream when
    you call one of the Read methods. If you manipulate the position of
    the underlying stream after reading data into the buffer, the position
    of the underlying stream might not match the position of the internal
    buffer. To reset the internal buffer, call the DiscardBufferedData
    method

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a method that takes either a StringReader instance (reading from the clipboard)
Aloha, I have a 8MB XML file that I wish to deserialize. I'm using
I have the following XML file: <?xml version=1.0?><!--This document contains the profiles that have
I just realized that TextReader, the type of Console.In, doesn't have a BeginRead-method. How
I have an XmlReader that is trying to read text into a list of
I have a text file in Program Files. I cannot write it from a
I have a web application which I uploaded using IIS. I want the users
I am trying to use a textreader to retrieve data from a text file
Background I have a few scripts that run as part of my build process
I have a list box that when an item is selected or deselected I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.