I made a program, Something very simple to backup 3 folders on my Hard Drive.
I use 3 Check boxes that I click that backups any one of 3 folders.
The problem that I have is that I want the files being copied to get placed into a Text Box or a List View Box. What I click the Backup button. It seems to copy all the files 1st before if it writes them into the Text Box.
I want it to write each file to the text box as it's being copied. Is that even possible.
any one know where I can get some samples using the background worker to copy file and write to a list box. ?
Loading

Kirtan PatelRank #292 · 8.6K reputationPosted Dec 6, 2009, 5:49 AM EST
Here is your Solution Please check "Do you like this answer check box if it helps you :)
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
Control.CheckForIllegalCrossThreadCalls = false;
string Foldertobackup = @"E:\backup";
string[] FilesInFolder = Directory.GetFiles(Foldertobackup);
Directory.CreateDirectory(@"D:\BackUpFolderName");
listBox1.Items.Add("Back up Directory Created !!" );
foreach (string file in FilesInFolder)
{
FileInfo f = new FileInfo(file);
File.Copy(file,@"D:\BackUpFolderName\"+f.Name,true);
listBox1.Items.Add(f.Name +"Copied \n\n!!") ;
}
}
private void button1_Click(object sender, EventArgs e)
{
backgroundWorker1.RunWorkerAsync();
}
Joe22 reputationPosted Dec 6, 2009, 6:26 AM EST
So I'm lost.
Here's my code below
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; string Foldertobackup = @"c:\\text\\textfiles\\"; string[] FilesInFolder = Directory.GetFiles(Foldertobackup); if(!Directory.Exists(Foldertobackup)) { Directory.CreateDirectory("c:\\backup\\text\\"); listBox1.Items.Add("Back up Directory Created !!" ); } foreach (string file in FilesInFolder) { FileInfo f = new FileInfo(file); File.Copy(Foldertobackup + file,@"c:\\BackUp\\text\\"+f.Name,true); listBox1.Items.Add(f.Name +"Copied \n\n!!") ; if (backgroundWorker1.CancellationPending) { e.Cancel = true; return; } } }Can you tell me what I'm going wrong
Kirtan PatelRank #292 · 8.6K reputationPosted Dec 6, 2009, 6:33 AM EST
just use single '\' in path
and friend, please check "Do you like this answer" check box :)
Joe22 reputationPosted Dec 6, 2009, 6:53 AM EST
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { Control.CheckForIllegalCrossThreadCalls = false; if(checkBox1.Checked) { string BackUpFolder = @"c:\backup\text\"; string Foldertobackup = @"c:\text\textfiles\"; string[] FilesInFolder = Directory.GetFiles(Foldertobackup); if(!Directory.Exists(BackUpFolder)) { Directory.CreateDirectory(BackUpFolder); listBox1.Items.Add("Back up Directory Created !!" ); } foreach (string file in FilesInFolder) { FileInfo f = new FileInfo(file); File.Copy(Foldertobackup + f.Name,BackUpFolder+f.Name,true); listBox1.Items.Add(f.Name +" Copied \n\n!!") ; if (backgroundWorker1.CancellationPending) { e.Cancel = true; return; } } } else { listBox1.Items.Add("TextFiles Not selected.\n\n!!") ; } }Joe22 reputationPosted Dec 6, 2009, 7:23 AM EST
Kirtan PatelRank #292 · 8.6K reputationPosted Dec 6, 2009, 8:44 AM EST
Code Copies Only One File at a Time As you can see Processing is Done in Foreach loop that process only one File at time :)
Joe22 reputationPosted Dec 6, 2009, 8:57 AM EST
Kirtan PatelRank #292 · 8.6K reputationPosted Dec 6, 2009, 9:03 AM EST
Joe22 reputationPosted Dec 6, 2009, 9:28 AM EST