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

  • Home
  • SEARCH
  • 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 7500399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:10:10+00:00 2026-05-29T20:10:10+00:00

I have the following basic code to read a text file from a StreamReader:

  • 0

I have the following basic code to read a text file from a StreamReader:

Using sr As New StreamReader(FileData)
  Dim line As String = String.Empty
  Do
    'Error occurs on the next line after looping 
    'past the *last* line in the file...sometimes
    line = sr.ReadLine()
    'Some code here to parse and process the line
  Loop Until (line Is Nothing) OrElse (line = String.Empty)
End Using

I tried changing the Do Loop to Do While Not sr.EndOfStream I tried checking If sr.ReadLine IsNot Nothing and a few other things but I keep getting the Cannot access a disposed object. Object name: ‘SslStream’ error after all 105 lines have successfully been read and processed. This code has been working for a couple of years, but there seems to be some issue with certain files (recently) that is causing this problem.

I opened the file and I didn’t see anything out of the ordinary. No extra blank lines, funny characters or anything of the like. Just a normal file with 105 properly formatted lines. So even know there is something about this file causing the error, I need to obviosly handle this programatically.

What can I do to prevent the sr.ReadLine() line of code from throwing that exception that I have not yet tried?

UPDATE Upon reading the last line of the file, the ‘EndOfStream’ property on the StreamReader goes immdeatly to: EndOfStream: {“Cannot access a disposed object.
Object name: ‘SslStream’.”}
It never toggles to ‘True’. This makes checking for this condition problematic.

I think I have figured out why this issue only occurs sometimes: if I add in a blank line to the file at the end, none of this ever occurs. It is only when the last line of the file contains data. Still, I should be able to handle this programatically. Telling the vendor supplying the file to always add a blank line at the end should not be the solution.

  • 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-29T20:10:12+00:00Added an answer on May 29, 2026 at 8:10 pm

    Finally – I got this one figured out. Some of the comments from my OP started make me look at this from a different angle. The underlying stream was automatically being closed. I started shifiting focus to the Stream object being loaded into the StreamReader rather than the StreamReader itself.

    Turns out the Stream was the culprit. The stream object has a .CanRead property which is ‘True’ until reading the last line of the file when it turns to ‘False’. This was causing the StreamReader.EndOfStream value to be an exception when there were no blank lines in the file.

    Checking the value of .CanRead on the underlying Stream which was loaded into the StreamReader and making sure it is not False was the solution. The new loop is below and I made sure to use the short-circuit ‘OrElse’ or the latter condition would thrown an exception as before.

    Using sr As New StreamReader(FileData)   
      Dim line As String = String.Empty   
      Do         
        line = sr.ReadLine()     
        'Some code here to parse and process the line   
      Loop Until (Not FileData.CanRead) OrElse (sr.EndOfStream) 
    End Using
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code to read a text file. const string FILENAME =
currently i have jdbc code with the following basic stucture: get Connection (do the
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
I'm trying to display an image using OpenCV. I have the following very basic
I have a website that uses Basic Authentication (username/password). Why is the following code
So far I have code to read an unsigned char from ifstream: ifstream in;
I have the following code, which is the basis for pagination in a larger
Say I have the following basic if-statement: if (A ~= 0) % do something
Probably a very basic beginner question. Imagine the following situation: I have an ASP.NET
I have following string String str = replace :) :) with some other string;

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.