Using Progress Bar In C#

Using Progress Bar in C#:
Progress Bar is very commonly used control in many applications to display the progress of task being performed. Progress Bar is displayed when copying files, deleting files or installing some software.
How to use Progress Bar:
·         Start New Windows Form Application.
·         Go to Toolbox.
·         Select Progress Bar and Drag it to your Form.

To make Progress Bar working you need to know some properties of Progress Bar
Progress Bar.Maximum: Defines how long it should run
Progress Bar.Step: Defines how much it should increase current position of Progress Bar.
Progress Bar.Style: How should it look like?
There is also a very basic method which is used to run Progress Bar properly.
Progress Bar.PerformStep()
Let’s see an example. In this example we will copy files and folders from one location to another and show the progress bar while copying.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace copynestedfolderformapplication
{
    public partial class Form1 : Form
    {
         int maxbytes = 0;
         int copied = 0;
         int total=0;
       
        public Form1()
        {
            InitializeComponent();
        }

        private void copybtn_Click(object sender, EventArgs e)
        {
            Copy1(@"F:\Posts", @"G:\copydata");
          
            MessageBox.Show("Done");
        }
       
        public void Copy1(string sourceDirectory, string targetDirectory)
        {
         
            DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
            DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
          //Gets size of all files present in source folder.
            GetSize(diSource, diTarget);
            maxbytes = maxbytes / 1024;
           
            progressBar1.Maximum = maxbytes;
            CopyAll(diSource, diTarget);
        }
        public void CopyAll(DirectoryInfo source, DirectoryInfo target)
        {
           
            if (Directory.Exists(target.FullName) == false)
            {
                Directory.CreateDirectory(target.FullName);
            }
            foreach (FileInfo fi in source.GetFiles())
            {
               
                fi.CopyTo(Path.Combine(target.ToString(), fi.Name), true);

                total += (int)fi.Length;
               
                copied += (int)fi.Length;
                copied /= 1024;
                progressBar1.Step =copied;
                            
                progressBar1.PerformStep();
                label1.Text = (total/1048576).ToString() + "MB of " + (maxbytes/1024).ToString() + "MB copied";
               
               
              
                label1.Refresh();
            }
            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
               
              
              
                DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
                CopyAll(diSourceSubDir, nextTargetSubDir);
            }
        }

        public  void GetSize(DirectoryInfo source, DirectoryInfo target)
        {

        
            if (Directory.Exists(target.FullName) == false)
            {
                Directory.CreateDirectory(target.FullName);
            }
            foreach (FileInfo fi in source.GetFiles())
            {
                maxbytes += (int)fi.Length;//Size of File
             
              
            }
            foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
            {
                DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
                GetSize(diSourceSubDir, nextTargetSubDir);
             
            }
           
        }
    }
}

It will display a Form like this after copying the files.


Comments

  1. Replies
    1. How you want to do this?

      Delete
    2. I understand that this is a beginners forum but you should let the users know that since you are running this on the UI Thread the progress bar and the label updates will not be updated until the process has completed.

      Delete
    3. Simple progressbar

      http://csharp.net-informations.com/gui/cs-progressbar.htm c# progressbar

      ling

      Delete
    4. Can i use progress bar functionality in a website. When a user logs in with his username and password and requests for reports which takes time to load as the data comes from database. What should i do for this?

      Delete
    5. Hi govinda its alot easier in website to show a progressbar you can use many of the available jquery plugins or simply you can show a .gif image of progress bar when data is loaded hide that image.

      Delete
  2. can i use to copy a single file

    ReplyDelete
    Replies
    1. Yes you can use it to copy single file instead of looping through files in directory you can directly give the path of the file

      Delete
  3. Great Awesome i m looking for this functionality for many days..
    Thank you so much.

    ReplyDelete

Post a Comment

Popular posts from this blog

Check if ViewBag is null or doesn't exist

Jquery serer side datatables in asp.net