1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
Imports System.ComponentModel
Imports System.IO
Public Class Form1
Inherits Form
Private WithEvents m_CopyWorker As BackgroundWorker
Private WithEvents m_TxbSource As TextBox
Private WithEvents m_TxbDestination As TextBox
Private WithEvents m_LblSource As Label
Private WithEvents m_LblDestination As Label
Private WithEvents m_BtnStartCancel As Button
Private WithEvents m_PgbCopyProgress As ProgressBar
Private WithEvents m_LblState As Label
Private WithEvents m_LblCopyState As Label
Public Sub New()
' instanciation du Bgw
m_CopyWorker = New BackgroundWorker With {.WorkerSupportsCancellation = True, .WorkerReportsProgress = True}
' instanciation et initialisation des contrôles
m_TxbSource = New TextBox With {.Name = "TxbSource", .Size = New Size(362, 20), .Location = New Point(106, 24)}
m_TxbDestination = New TextBox With {.Name = "TxbDestination", .Size = New Size(362, 20), .Location = New Point(106, 50)}
m_LblSource = New Label With {.Name = "LblSource", .AutoSize = True, .Text = "Source :", .Size = New Size(47, 13), .Location = New Point(31, 27)}
m_LblDestination = New Label With {.Name = "LblDestination", .AutoSize = True, .Text = "Destination :", .Size = New Size(66, 13), .Location = New Point(12, 53)}
m_BtnStartCancel = New Button With {.Name = "BtnStartCancel", .Text = "Lancer la copie", .Size = New Size(362, 23), .Location = New Point(106, 93)}
m_PgbCopyProgress = New ProgressBar With {.Name = "PgbCopyProgress", .Size = New Size(362, 23), .Location = New Point(106, 159)}
m_LblState = New Label With {.Name = "LblState", .AutoSize = True, .Text = "Etat :", .Size = New Size(32, 13), .Location = New Point(46, 133)}
m_LblCopyState = New Label With {.Name = "LblCopyState", .AutoSize = False, .Text = "", .Size = New Size(365, 23), .Location = New Point(103, 133)}
' Initialisation du Container de type Form1 et ajout des contrôles
Me.Name = "Form1"
Me.Text = "Form1"
Me.AutoScaleDimensions = New SizeF(6.0!, 13.0!)
Me.AutoScaleMode = AutoScaleMode.Font
Me.ClientSize = New Size(521, 196)
Me.Controls.Add(m_LblCopyState)
Me.Controls.Add(m_LblState)
Me.Controls.Add(m_PgbCopyProgress)
Me.Controls.Add(m_BtnStartCancel)
Me.Controls.Add(m_LblDestination)
Me.Controls.Add(m_LblSource)
Me.Controls.Add(m_TxbDestination)
Me.Controls.Add(m_TxbSource)
End Sub
Private Sub m_BtnStartCancel_Click(sender As Object, e As EventArgs) Handles m_BtnStartCancel.Click
If m_BtnStartCancel.Text = "Lancer la copie" Then
If Directory.Exists(m_TxbSource.Text) = False OrElse Directory.Exists(m_TxbDestination.Text) = False Then
MsgBox("Veuillez saisir un chemin valide !")
Else
If Not m_CopyWorker.IsBusy Then
m_BtnStartCancel.Text = "Annuler la copie"
m_CopyWorker.RunWorkerAsync(Tuple.Create(m_TxbSource.Text, m_TxbDestination.Text))
End If
End If
ElseIf m_BtnStartCancel.Text = "Annuler la copie" Then
m_CopyWorker.CancelAsync()
End If
End Sub
Private Sub m_CopyWorker_RunWorkerAsync(sender As Object, e As DoWorkEventArgs) Handles m_CopyWorker.DoWork
Dim Tuples As Tuple(Of String, String) = TryCast(e.Argument, Tuple(Of String, String))
Dim dir As DirectoryInfo = New DirectoryInfo(Tuples.Item1)
' On retourne le nombre de fichiers total dans le chemin source y compris ceux des sous répertoires
Dim allFiles() As FileInfo = dir.GetFiles("*", SearchOption.AllDirectories)
Dim FilesCount% = allFiles.Count
' On déclare et initie une variable local qui sera passsée par référence à la méthode DirectoryCopy de manière à incrémenter correctement dans la récursivité
Dim LocalFilesCount% = 0
Copy.DirectoryCopy(Tuples.Item1, Tuples.Item2, FilesCount, LocalFilesCount, m_CopyWorker, e)
End Sub
Private Sub m_CopyWorker_ProgressChanged(sender As Object, e As ProgressChangedEventArgs) Handles m_CopyWorker.ProgressChanged
' On mets à jour l'UI
m_PgbCopyProgress.Value = e.ProgressPercentage
m_LblCopyState.Text = e.UserState.ToString
End Sub
Private Sub m_CopyWorker_RunWorkerCompleted(sender As Object, e As RunWorkerCompletedEventArgs) Handles m_CopyWorker.RunWorkerCompleted
' On gère ce qui a été déclenché à l'issue ou lors de la copie
If e.Cancelled Then
MsgBox("Copie annulée")
ElseIf e.Error IsNot Nothing Then
MsgBox("erreur lors de la copie")
Else
MsgBox(e.Result(0) & vbNewLine & e.Result(1))
End If
' On réinitialise les contrôles
m_PgbCopyProgress.Value = 0
m_LblCopyState.Text = String.Empty
m_BtnStartCancel.Text = "Lancer la copie"
End Sub
End Class |