Click here to Skip to main content
16,012,110 members

Comments by AAMERSAEED (Top 12 by date)

AAMERSAEED 13-Feb-12 16:34pm View    
Thank you:)......i've done it.
AAMERSAEED 19-Dec-11 12:44pm View    
Actually now i am at home and i've win xp on my pc and i check this progress bar is working well i've checked it on 200 kb folder but in university on window 7 it appears that progress bar not finishes but stop just one step before.ie slightly before the end point??????Also suggest me if what i am doing above in this application is correct approach or not??? because i've also heard about background worker that is used for progress bars???????if i use that i don't know which code of my current application should be placed in Do_Work event???????How ever i know that do_work event contains that code which is hard task and needs to run in the background??????Please suggest me the professional way???I will always appreciate your help as your suggestion in this regard is helpfull for me.
AAMERSAEED 18-Dec-11 18:42pm View    
Hi Mika Wendelius,
First of all thanks for the cooperation. This is what i am doing and it is working but the problem is that some time progress bar stuck at the end specifically when it comes to those files which are smaller in size. Can i post the image here also??
<pre lang="c#">
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 ProgressBarWithinClass
{
class ClassDir
{

static int maxbytes = 0;
static int copied = 0;
static int total = 0;

public static void Copy1(Label label1,ProgressBar progressBar1,string sourceDirectory, string targetDirectory)
{
DirectoryInfo diSource = new DirectoryInfo(sourceDirectory);
DirectoryInfo diTarget = new DirectoryInfo(targetDirectory);
GetSize(diSource, diTarget);
maxbytes = maxbytes / 1024;

progressBar1.Maximum = maxbytes;
CopyAll(label1,progressBar1,diSource, diTarget);


}
public static void CopyAll(Label lable1, ProgressBar progressBar1,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.Value=copied;
progressBar1.PerformStep();


lable1.Text = (total / 1024).ToString() + "KB of " + maxbytes.ToString() + "KB copied";
lable1.Refresh();



}
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{



DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);

CopyAll(lable1, progressBar1, diSourceSubDir, nextTargetSubDir);
}

}

public static void GetSize(DirectoryInfo source, DirectoryInfo target)
{


foreach (FileInfo fi in source.GetFiles())
{
maxbytes += (int)fi.Length;

}
foreach (DirectoryInfo diSourceSubDir in source.GetDirectories())
{
DirectoryInfo nextTargetSubDir = target.CreateSubdirectory(diSourceSubDir.Name);
GetSize(diSourceSubDir, nextTargetSubDir);

}
}

}
}
</pre>
And this is separate class i.e Form Class

<pre lang="c#">


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 ProgressBarWithinClass
{
public partial class Form1 : Form
{
ProgressBar pBar;
Form mainform;
Label l;

public Form1()
{
InitializeComponent();
pBar = new ProgressBar();
pBar.Location = new System.Drawing.Point(20, 20);
pBar.Name = "progressBar1";
pBar.Width = 200;

pBar.Height = 30;
Controls.Add(pBar);
l = new Label();
l.Name = "label1";
l.Location = new System.Drawing.Point(50,50);
l.Height=50;
Controls.Add(l);







}

private void copybtn_Click(object sender, EventArgs e)
{
ClassDir.Copy1(l,pBar,@"D:\Sajjad", @"E:\copydata");
//CopyDir.GetInfo();
AAMERSAEED 18-Dec-11 14:49pm View    
Will you please more elaborate it for me???????Give me example?
AAMERSAEED 17-Dec-11 8:50am View    
Another question if you don't mind please tell me how can i control the progress bar because I've placed this recursive function of copy nested folder in the separate class named "CopyDir" and all the methods are static as well........so basically there are two classes 1. Form class which contains the progress bar 2. CopyDir class.
what to do so that progress bar keep ticking. Should i pass the progress bar as or the whole form to the CopyDir class??????????????