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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:52:52+00:00 2026-05-12T13:52:52+00:00

In my C# app I’m trying to feed into ReadLine() a simple text document

  • 0

In my C# app I’m trying to feed into ReadLine() a simple text document with 7 digit strings separated line by line. What I’m attempting to do is grab the next 7 digit string each time the function is called. Here’s what I have so far:

string invoiceNumberFunc()
    {
        string path = @"C:\Users\sam\Documents\GCProg\testReadFile.txt";
        try
        {
            using (StreamReader sr = new StreamReader(path))
            {
                invoiceNumber = sr.ReadLine();

            }

        }
        catch (Exception exp)
        {
            Console.WriteLine("The process failed: {0}", exp.ToString());
        }
       return invoiceNumber;
    }

How do I advance to the next line each time the invoiceNumberFunc() is called?

Thanks in advance.

  • 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-12T13:52:52+00:00Added an answer on May 12, 2026 at 1:52 pm

    You’d need to keep hold of the StreamReader between calls, either passing it into the method as a new parameter or making it a member variable of the class.

    Personally I prefer the idea of it becoming a parameter, so that it never ends up as a member variable – that makes the life-time easier to manage:

    void DoStuff()
    {
        string path = @"C:\Users\sam\Documents\GCProg\testReadFile.txt";
        using (StreamReader sr = new StreamReader(path))
        {
            while (keepGoing) // Whatever logic you have
            {
                string invoice = InvoiceNumberFunc(sr);
                // Use invoice
            }
        }
    }
    
    string InvoiceNumberFunc(TextReader reader)
    {
        string invoiceNumber;
        try
        {
            invoiceNumber = reader.ReadLine();
        }
        catch (Exception exp)
        {
            Console.WriteLine("The process failed: {0}", exp.ToString());
        }
        return invoiceNumber;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My app will be full screen, but I am having trouble finding the document
The app I am trying to make needs a few info in the beggining,
The app I am developing has many activities organized into seven java packages. Originally
My app has many controls on its surface, and more are added dynamically at
An app I'm writing always crashes on a clients computer, but I don't get
My app is installed via NSIS. I want the installer to install the program
My app uses a WebRequest at certain points to get pages from itself. This
My app keeps track of the state of about 1000 objects. Those objects are
Our app (already deployed) is using an Access/Jet database. The upcoming version of our
My app has a DataGridView object and a List of type MousePos. MousePos is

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.