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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:21:18+00:00 2026-05-27T15:21:18+00:00

I have a function which reads a big text file,splits a part(from a given

  • 0

I have a function which reads a big text file,splits a part(from a given start and end),and save the splitted data into another text file.since the file is too big,i need to add a progressbar when reading the stream and another one when writing the splitted text into the other file.Ps.start and end are given datetime!!

using (StreamReader sr = new StreamReader(file,System.Text.Encoding.ASCII))
{
    while (sr.EndOfStream == false)
    {
        line = sr.ReadLine();

        if (line.IndexOf(start) != -1)
        {
            using (StreamWriter sw = new StreamWriter(DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + "cut"))
            {
                sw.WriteLine(line);
                while (sr.EndOfStream == false && line.IndexOf(end) == -1)
                {
                    line = sr.ReadLine();
                    sw.WriteLine(line);
                }
            }

            richTextBox1.Text += "done ..." + "\n";
            break;
        }
    }
}
  • 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-27T15:21:19+00:00Added an answer on May 27, 2026 at 3:21 pm

    The first thing to do would be to work out how long the file is using FileInfo,

    http://msdn.microsoft.com/en-us/library/system.io.fileinfo.aspx

    FileInfo fileInfo = new FileInfo(file);
    long length = fileInfo.Length;
    

    I would suggest you do it like this,

    private long currentPosition = 0;
    
    private void UpdateProgressBar(int lineLength)
    {
        currentPosition += line.Count; // or plus 2 if you need to take into account carriage return
        progressBar.Value = (int)(((decimal)currentPosition / (decimal)length) * (decimal)100);
    }
    
    private void CopyFile()
    {
        progressBar.Minimum = 0;
        progressBar.Maximum = 100;
    
        currentPosition = 0;
    
        using (StreamReader sr = new StreamReader(file,System.Text.Encoding.ASCII))
        {
            while (sr.EndOfStream == false)
            {
                line = sr.ReadLine();
                UpdateProgressBar(line.Length);
    
                if (line.IndexOf(start) != -1)
                {
                    using (StreamWriter sw = new StreamWriter(DateTime.Now.ToString().Replace("/", "-").Replace(":", "-") + "cut"))
                    {
                        sw.WriteLine(line);
                        while (sr.EndOfStream == false && line.IndexOf(end) == -1)
                        {
                            line = sr.ReadLine();
                            UpdateProgressBar(line.Length);
                            sw.WriteLine(line);
                        }
                    }
    
                    richTextBox1.Text += "done ..." + "\n";
                    break;
                }
            }
        }
    }
    

    Which is calculating the percentage of the file that has been read and setting the progress bar to that value. Then you don’t have to worry about whether the length is a long, and the progress bar uses int.

    If you don’t want to truncate the value then do this (casting to an int above will always truncate the decimals, and thus round down),

    progressBar.Value = (int)Math.Round(((decimal)currentPosition / (decimal)length) * (decimal)100), 0);
    

    Is this on a background thread? Don’t forget that you will have to call this.Invoke to update the progress bar or else you will get a cross thread exception.

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

Sidebar

Related Questions

I have a function TRANSLATE which reads value from the xml file based on
I have a function which first reads an image from disk, resizes it and
Let's say I want to have a function which reads data from the SerialPort
I have the following function which reads from a firebird database. The Function works
I have a program which reads an XML file using the DOM functions: $doc
I have a function which gets a key from the user and generates a
i have a function which retrieves values from a webservice , then loops through
I have the following function which works very well within a $(document).ready(function(){ $('.threadWrapper >
I have the following jQuery which I need adapting: $(document).ready(function(){ $(.rss-popup a).hover(function() { $(this).next(em).stop(true,
I have a function which parses one string into two strings. In C# 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.