The simulation can go in such way.
Create a delete to referesh UI.
From you presentation layer start background worker.
In that call a method which actually identify location and count total number of files to be copied using below function.
By using this we can count files...
public static int CountFiles(string source,string destination)
{
int fileCount = 0;
Stack<Folder> stack = new Stack<Folder>();
stack.Push(new Folder(source, destination));
while (stack.Count > 0)
{
var path = stack.Pop();
foreach (string filepath in Directory.GetFiles(path.sourcePath, "*.*"))
{
fileCount++;
}
foreach (string dir in Directory.GetDirectories(path.sourcePath))
{
stack.Push(new Folder(dir, destination));
}
}
_fileCount = fileCount;
return fileCount;
}
According to my approch, I have created a class and used stack for counting and coying file which is more faster then recursive technique.
Then from Background worker only or from the same method start coying.
public static void CopyFiles(ReportProgressWorker worker,string source, string destination)
{
_avgTime = new TimeSpan();
TimeSpan start, end;
string time=string.Empty;
Stack<Folder> stack = new Stack<Folder>();
stack.Push(new Folder(source, destination));
copiedFile = worker;
while (stack.Count > 0)
{
var path=stack.Pop();
string dirpath=Path.Combine(path.destPath,Path.GetFileName(path.sourcePath));
if(!Directory.Exists(dirpath))
Directory.CreateDirectory(dirpath);
foreach (string filepath in Directory.GetFiles(path.sourcePath,"*.*"))
{
string targetpath=Path.Combine(dirpath,Path.GetFileName(filepath));
if (File.Exists(targetpath))
File.Delete(targetpath);
start = DateTime.Now.TimeOfDay;
File.Copy(filepath, targetpath);
_processedfiles++;
end = DateTime.Now.TimeOfDay;
string temp = ComputeTimeRequired(start, end);
if (!string.IsNullOrEmpty(temp))
time = temp;
copiedFile(Path.GetFileName(targetpath),time);
}
foreach (string dir in Directory.GetDirectories(path.sourcePath))
{
stack.Push(new Folder(dir, Path.Combine(dirpath, Path.GetFileName(dir))));
}
}
}
This copy and update together.
public void ReportTheProgress(string prg,string timeStatus)
using this progess change event update progess bar. I think it will do all.
If I am wrong anywhere please correct me guys.
Thanks