-4

I am new in C# programming and I have a hard time on how to put a progress bar on my project file. Could you help me solve this one?.

Here is my code below:

try
{
    lblUpdate.Visible = true;
    lblUpdate.Refresh();

    string[] filenames = Directory.GetFiles(sTargetFolderPath);

    // Zip up the files - From SharpZipLib Demo Code
    using (ZipOutputStream s = new ZipOutputStream(File.Create(lblSaveTo.Text + "\\" + sZipFileName + ".pld")))
    {
        s.SetLevel(9); // 0-9, 9 being the highest level of compression

        byte[] buffer = new byte[4096];

        foreach (string file in filenames)
        {

            ZipEntry entry = new ZipEntry(Path.GetFileName(file));

            entry.DateTime = DateTime.Now;
            s.PutNextEntry(entry);

            using (FileStream fs = File.OpenRead(file))
            {
                int sourceBytes;
                do
                {
                    sourceBytes = fs.Read(buffer, 0, buffer.Length);
                    s.Write(buffer, 0, sourceBytes);

                } while (sourceBytes > 0);
            }
        }
        s.Finish();
        s.Close();
    }
}
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
Botsokoy91
  • 5
  • 1
  • 6
  • 1
    Here is a very similar question with a good answer that I think you'll find helpful: http://stackoverflow.com/questions/6044629/file-copy-with-progress-bar?rq=1 – Scott Miller Aug 31 '12 at 21:16

2 Answers2

0

You have to use a backgroundworker in order not to freeze UI when it process file set in DoWork method your process and use Progress event to set your progress bar here an article that explains how http://www.dotnetperls.com/progressbar set your progressbar Maximum property to number of files that is filenames.Length

    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Shown += new EventHandler(Form1_Shown);

            // To report progress from the background worker we need to set this property
            backgroundWorker1.WorkerReportsProgress = true;
            // This event will be raised on the worker thread when the worker starts
            backgroundWorker1.DoWork += new DoWorkEventHandler(backgroundWorker1_DoWork);
            // This event will be raised when we call ReportProgress
            backgroundWorker1.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker1_ProgressChanged);
        }
        void Form1_Shown(object sender, EventArgs e)
        {
            // Start the background worker
            backgroundWorker1.RunWorkerAsync();
        }
        // On worker thread so do our thing!
        void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
           int i = 0; 
            foreach (string file in filenames)    
            {
                i++;
                // Report progress to 'UI' thread
                backgroundWorker1.ReportProgress(i);
               // Your background task goes here zip files
            }
        }
        // Back on the 'UI' thread so we can update the progress bar
        void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            // The progress percentage is a property of e
            progressBar1.Value = e.ProgressPercentage;
        }
    }
Niko
  • 26,516
  • 9
  • 93
  • 110
Hassan Boutougha
  • 3,871
  • 1
  • 17
  • 17
-1

Make a progress bar object from the tool books (just drag and drop {Or you could make one yourself via the code whatever ya want})

then Take the number of files you run on, (100 / nNumOfFiles) and progress the bar by that amount each iteration of the loop here is the complited code where 'prbProgressBar' is the progress bar

    try
{

lblUpdate.Visible = true;
lblUpdate.Refresh();
string[] filenames = Directory.GetFiles(sTargetFolderPath);

// Zip up the files - From SharpZipLib Demo Code
using (ZipOutputStream s = new ZipOutputStream(File.Create(lblSaveTo.Text + "\\" + sZipFileName + ".pld")))
{
    s.SetLevel(9); // 0-9, 9 being the highest level of compression

    byte[] buffer = new byte[4096];
    int nPercentToAdvance = (100 / filenames.Length);
    foreach (string file in filenames)
    {

        ZipEntry entry = new ZipEntry(Path.GetFileName(file));

        entry.DateTime = DateTime.Now;
        s.PutNextEntry(entry);

        using (FileStream fs = File.OpenRead(file))
        {
            int sourceBytes;
            do
            {
                sourceBytes = fs.Read(buffer, 0, buffer.Length);
                s.Write(buffer, 0, sourceBytes);

            } while (sourceBytes > 0);
        }

        this.prbProgressBar.Value += nPercentToAdvance;
    }
    s.Finish();
    s.Close();
}

}

David Limkys
  • 4,907
  • 5
  • 26
  • 38
  • 1
    -1 Did you test this? The UI will not paint until the code behind runs and the prbProgressBar.Value will only display the final value. – paparazzo Aug 31 '12 at 22:43
  • The progress bar still don't start processing. I think there is something lacking. But I don't know which part. – Botsokoy91 Sep 01 '12 at 11:20
  • @Botsokoy91 Did you like read my comment? It is an explanation of why it is not progressing. See the correct answer from Hassan? – paparazzo Sep 01 '12 at 18:51