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 7654661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T12:18:52+00:00 2026-05-31T12:18:52+00:00

I have a problem that I have been trying to figure out for hours

  • 0

I have a problem that I have been trying to figure out for hours now, and I am sure it’s something simple.

Here is the code (extra junk removed as it’s not a problem).

foreach (String itemChecked in fightsList.CheckedItems)
{
    try
    {
        Thread.Sleep(50);
        using (StreamReader reader = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.ASCII))
            {
                while ((line = reader.ReadLine()) != null)
                    {
                        // do all stuff to lines here...
                    }
            }
    }
    catch (Exception error)
        {
            errorText.Text = error.ToString();
        }
}
// foreach done here.

I am reading a text file, obvious from StreamReader, but when the foreach loop rolls to the second iteration, it fails before running the StreamReader and stops the foreach loop.

The first loop runs great.

I am thinking it’s a problem because the ‘StreamReader reader = new’ already exists and it cannot create a new one?

The foreach is a string checkedBox. StreamReader is reading the same file for all instances in the loop, I just handle where to start and stop per each. My error catching does not get anything, and the application does not crash.

  • 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-31T12:18:53+00:00Added an answer on May 31, 2026 at 12:18 pm

    The problem is not in the code you have posted. I would suggest allocating the file stream as a separate step and putting a break point on the line where you are passing it into the StreamReader. On the second pass make sure everything (filename, length, etc) looks OK on the underlying stream your problem is inside of the loop otherwise it is elsewhere in your code.

    your also better off reorganizing your code like

    using(StreamReader reader = new StreamReader(new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite), Encoding.ASCII)){
        while ((line = reader.ReadLine()) != null)
        {
            foreach (String itemChecked in fightsList.CheckedItems)
           {
           }
        }
    }
    

    since this will make the expensive operation (file IO) happen as few times as possible, but that isn’t why you are having this specific problem.

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

Sidebar

Related Questions

Been sitting here for hours now trying to figure this out, so a bit
This is something that I have been trying to figure out for a while
I've been trying to figure this out for a few hours now. In order
I've been trying to figure this out for a couple days now. I have
I have been trying to figure this problem out without asking for help, but
I have been trying to figure this out and know that overriding the volume
I have been Googling a problem that I have with trying to integrate the
I have been trying to figure out a way to convert bitmap files into
I have been trying to figure out how to associate data with the event
I am fairly new to Emacs and I have been trying to figure out

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.