Backgroundworker: Copy Filestypes with progressbar

Ultrawhack

Well-known member
Joined
Jul 5, 2006
Messages
164
Location
Canada
Programming Experience
3-5
DragDrop copy files backgroundworker with progressbar

I'd like to offer users the ability to copy files to relevant hardcoded app folder by dragging & dropping on my form and have them copied using a backgroundworker with progressbar & cancel (rather than locking up the app) - similar to windows filecopy behaviour.

I found these sample code elsewhere. My knowledge is limited so can someone help me doctor this code to make it suitable for background filecopy ?
http://www.codeproject.com/cs/miscctrl/progressdialog.asp

I also found this directory copy code which would be great if I could get it all to work together.
http://www.codeproject.com/cs/files/xdirectorycopy.asp

Thanks for any help !
 
With reference to this codeblock below, how can I run the 2nd status form as a msn messenger-type sliding taskbar popup like
http://www.codeproject.com/cs/miscctrl/taskbarnotifier.asp

Thanks for any help...

VB.NET:
[/COLOR][/FONT]
[FONT='Courier New'][COLOR=blue][COLOR=blue][FONT='Courier New']Imports[/FONT][/COLOR][FONT='Courier New'] System.io[/FONT]
[COLOR=blue][FONT='Courier New']Public[/FONT][/COLOR][FONT='Courier New'] [COLOR=blue]Class[/COLOR] Form1[/FONT]
[FONT='Courier New'][/FONT] 
[FONT='Courier New']    [COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] Form1_Load([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] [COLOR=blue]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.EventArgs) _[/FONT]
[FONT='Courier New']    [COLOR=blue]Handles[/COLOR] [COLOR=blue]Me[/COLOR].Load[/FONT]
[FONT='Courier New']        [COLOR=green]' Hide this button[/COLOR][/FONT]
[FONT='Courier New']        CancelCopying.Visible = [COLOR=blue]False[/COLOR][/FONT]
[FONT='Courier New']    [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
[FONT='Courier New']    [COLOR=green]' The CancelCopying button sits exactly on top of[/COLOR][/FONT]
[FONT='Courier New']    [COLOR=green]' the StartCopying button. So we can see one or the[/COLOR][/FONT]
[FONT='Courier New']    [COLOR=green]' other, but not both.[/COLOR][/FONT]
[COLOR=green][FONT='Courier New'][/FONT][/COLOR] 
[FONT='Courier New']    [COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] BackgroundWorker1_DoWork([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.Object, _[/FONT]
[FONT='Courier New']    [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.ComponentModel.DoWorkEventArgs) _[/FONT]
[FONT='Courier New']    [COLOR=blue]Handles[/COLOR] BackgroundWorker1.DoWork[/FONT]
[FONT='Courier New']        [COLOR=green]' Because 'f2' is created on the background thread[/COLOR][/FONT]
[FONT='Courier New']        [COLOR=green]' We will have no cross thread problems updating it[/COLOR][/FONT]
[FONT='Courier New']        [COLOR=green]' from here.[/COLOR][/FONT]
[COLOR=green][FONT='Courier New'][/FONT][/COLOR] 
[FONT='Courier New']        [COLOR=green]' Form2 though was created through the IDE.[/COLOR][/FONT]
[FONT='Courier New']        [COLOR=blue]Dim[/COLOR] f2 [COLOR=blue]As[/COLOR] [COLOR=blue]New[/COLOR] Form2[/FONT]
[FONT='Courier New']        f2.ProgressBar1.Minimum = 1[/FONT]
[FONT='Courier New']        f2.ProgressBar1.Value = 1[/FONT]
[FONT='Courier New']        f2.ProgressBar1.Step = 1[/FONT]
[FONT='Courier New']        f2.TopMost = [COLOR=blue]True[/COLOR][/FONT]
[FONT='Courier New']        f2.Show()[/FONT]
[FONT='Courier New'][/FONT] 
[FONT='Courier New']        [COLOR=blue]Dim[/COLOR] theExtentions() [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = {[COLOR=maroon]"*.jpg"[/COLOR], [COLOR=maroon]"*.bmp"[/COLOR]}[/FONT]
[FONT='Courier New']        f2.Label1.Text = [COLOR=maroon]"Please wait ... Gathering file information"[/COLOR][/FONT]
[FONT='Courier New']        Application.DoEvents()[/FONT]
[FONT='Courier New']        f2.Refresh()[/FONT]
[FONT='Courier New']        [COLOR=blue]For[/COLOR] [COLOR=blue]Each[/COLOR] currentExt [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] [COLOR=blue]In[/COLOR] theExtentions[/FONT]
[FONT='Courier New']            [COLOR=blue]Dim[/COLOR] theFiles() [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] = _[/FONT]
[FONT='Courier New']            Directory.GetFiles _[/FONT]
[FONT='Courier New']            (Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), _[/FONT]
[FONT='Courier New']            currentExt, SearchOption.AllDirectories)[/FONT]
[FONT='Courier New']            f2.ProgressBar1.Maximum = theFiles.Length[/FONT]
[FONT='Courier New']            [COLOR=blue]For[/COLOR] [COLOR=blue]Each[/COLOR] currentFile [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR] [COLOR=blue]In[/COLOR] theFiles[/FONT]
[FONT='Courier New']                [COLOR=blue]Try[/COLOR][/FONT]
[FONT='Courier New']                    f2.Label1.Text = [COLOR=maroon]"Copying: "[/COLOR] & currentFile[/FONT]
[FONT='Courier New'][/FONT] 
[FONT='Courier New']                    [COLOR=blue]My[/COLOR].Computer.FileSystem.CopyFile(currentFile, _[/FONT]
[FONT='Courier New']               [COLOR=maroon]"c:\tempfiles\"[/COLOR] & System.IO.Path.GetFileName(currentFile), [COLOR=blue]True[/COLOR])[/FONT]
[FONT='Courier New']                [COLOR=blue]Catch[/COLOR] ex [COLOR=blue]As[/COLOR] Exception[/FONT]
[FONT='Courier New']                    MsgBox([COLOR=maroon]"Error copying "[/COLOR] & currentFile)[/FONT]
[FONT='Courier New']                [COLOR=blue]End[/COLOR] [COLOR=blue]Try[/COLOR][/FONT]
[FONT='Courier New']                f2.ProgressBar1.PerformStep()[/FONT]
[FONT='Courier New']                Application.DoEvents()[/FONT]
[FONT='Courier New']                f2.Refresh()[/FONT]
[FONT='Courier New']                [COLOR=blue]If[/COLOR] BackgroundWorker1.CancellationPending [COLOR=blue]Then[/COLOR][/FONT]
[FONT='Courier New']                    e.Cancel = [COLOR=blue]True[/COLOR][/FONT]
[FONT='Courier New']                    [COLOR=blue]Exit[/COLOR] [COLOR=blue]For[/COLOR][/FONT]
[FONT='Courier New']                [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
[FONT='Courier New']            [COLOR=blue]Next[/COLOR][/FONT]
[FONT='Courier New']            f2.ProgressBar1.Value = 1[/FONT]
[FONT='Courier New']            [COLOR=blue]If[/COLOR] BackgroundWorker1.CancellationPending [COLOR=blue]Then[/COLOR][/FONT]
[FONT='Courier New']                e.Cancel = [COLOR=blue]True[/COLOR][/FONT]
[FONT='Courier New']                [COLOR=blue]Exit[/COLOR] [COLOR=blue]For[/COLOR][/FONT]
[FONT='Courier New']            [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
[FONT='Courier New']        [COLOR=blue]Next[/COLOR][/FONT]
[COLOR=blue][FONT='Courier New'][/FONT][/COLOR] 
[FONT='Courier New']        f2.Dispose()[/FONT]
[FONT='Courier New']    [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
[COLOR=blue][FONT='Courier New'][/FONT][/COLOR] 
[COLOR=blue][FONT='Courier New'][/FONT][/COLOR] 
[FONT='Courier New']    [COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] StartCopying_Click([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.Object, _[/FONT]
[FONT='Courier New']    [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.EventArgs) [COLOR=blue]Handles[/COLOR] StartCopying.Click[/FONT]
[FONT='Courier New'][/FONT] 
[FONT='Courier New']        [COLOR=blue]Me[/COLOR].BackgroundWorker1.WorkerSupportsCancellation = [COLOR=blue]True[/COLOR][/FONT]
[FONT='Courier New']        [COLOR=blue]Me[/COLOR].BackgroundWorker1.RunWorkerAsync()[/FONT]
[FONT='Courier New']        CancelCopying.Visible = [COLOR=blue]True[/COLOR][/FONT]
[COLOR=blue][FONT='Courier New'][/FONT][/COLOR] 
[FONT='Courier New']    [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
[COLOR=blue][FONT='Courier New'][/FONT][/COLOR] 
[FONT='Courier New']    [COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] CancelCopying_Click([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] System.Object, [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.EventArgs) [COLOR=blue]Handles[/COLOR] CancelCopying.Click[/FONT]
[FONT='Courier New']        BackgroundWorker1.CancelAsync()[/FONT]
[FONT='Courier New']        CancelCopying.Visible = [COLOR=blue]False[/COLOR][/FONT]
[FONT='Courier New']    [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
[FONT='Courier New'][COLOR=blue][/COLOR][/FONT] 
[FONT='Courier New']    [COLOR=blue]Private[/COLOR] [COLOR=blue]Sub[/COLOR] Form1_FormClosing([COLOR=blue]ByVal[/COLOR] sender [COLOR=blue]As[/COLOR] [COLOR=blue]Object[/COLOR], [COLOR=blue]ByVal[/COLOR] e [COLOR=blue]As[/COLOR] System.Windows.Forms.FormClosingEventArgs) [COLOR=blue]Handles[/COLOR] [COLOR=blue]Me[/COLOR].FormClosing[/FONT]
[FONT='Courier New']        [COLOR=blue]If[/COLOR] BackgroundWorker1.IsBusy [COLOR=blue]Then[/COLOR][/FONT]
[FONT='Courier New']            [COLOR=blue]If[/COLOR] MessageBox.Show([COLOR=maroon]"Copying still in progress. Do you want to cancel it?"[/COLOR], _[/FONT]
[FONT='Courier New']            [COLOR=maroon]"Cancel background work?"[/COLOR], MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes [COLOR=blue]Then[/COLOR][/FONT]
[FONT='Courier New']                BackgroundWorker1.CancelAsync()[/FONT]
[FONT='Courier New']            [COLOR=blue]Else[/COLOR][/FONT]
[FONT='Courier New']                [COLOR=green]' Cancel the form close event[/COLOR][/FONT]
[FONT='Courier New']                e.Cancel = [COLOR=blue]True[/COLOR][/FONT]
[FONT='Courier New']            [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
[FONT='Courier New']        [COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR][/FONT]
[FONT='Courier New']    [COLOR=blue]End[/COLOR] [COLOR=blue]Sub[/COLOR][/FONT]
[COLOR=blue][FONT='Courier New']End[/FONT][/COLOR][FONT='Courier New'] [COLOR=blue]Class[/COLOR][/FONT]
[/COLOR][/FONT][FONT='Courier New'][COLOR=blue]
 
Back
Top