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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
Imports System.IO
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " INITIALIZE "
Public Sub New()
InitializeComponent()
End Sub
'Form remplace la méthode Dispose pour nettoyer la liste des composants.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Requise par le Concepteur Windows Form
Private components As System.ComponentModel.IContainer
'REMARQUE*: la procédure suivante est requise par le Concepteur Windows Form
'Elle peut être modifiée à l'aide du Concepteur Windows Form.
'Ne la modifiez pas à l'aide de l'éditeur de code.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.GbxCopySetting = New System.Windows.Forms.GroupBox()
Me.LblCopySettingTo = New System.Windows.Forms.Label()
Me.LblCopySettingFrom = New System.Windows.Forms.Label()
Me.TxbCopySettingTo = New System.Windows.Forms.TextBox()
Me.BtnCopySettingTo = New System.Windows.Forms.Button()
Me.TxbCopySettingFrom = New System.Windows.Forms.TextBox()
Me.BtnCopySettingFrom = New System.Windows.Forms.Button()
Me.GbxCopyProgress = New System.Windows.Forms.GroupBox()
Me.LblCopyProgress = New System.Windows.Forms.Label()
Me.PgbLblCopyProgress = New System.Windows.Forms.ProgressBar()
Me.BgwCopying = New System.ComponentModel.BackgroundWorker()
Me.BtnCopy = New System.Windows.Forms.Button()
Me.GbxCopySetting.SuspendLayout()
Me.GbxCopyProgress.SuspendLayout()
Me.SuspendLayout()
'
'GbxCopySetting
'
Me.GbxCopySetting.Controls.Add(Me.LblCopySettingTo)
Me.GbxCopySetting.Controls.Add(Me.LblCopySettingFrom)
Me.GbxCopySetting.Controls.Add(Me.TxbCopySettingTo)
Me.GbxCopySetting.Controls.Add(Me.BtnCopySettingTo)
Me.GbxCopySetting.Controls.Add(Me.TxbCopySettingFrom)
Me.GbxCopySetting.Controls.Add(Me.BtnCopySettingFrom)
Me.GbxCopySetting.Location = New System.Drawing.Point(12, 12)
Me.GbxCopySetting.Name = "GbxCopySetting"
Me.GbxCopySetting.Size = New System.Drawing.Size(541, 92)
Me.GbxCopySetting.TabIndex = 8
Me.GbxCopySetting.TabStop = False
Me.GbxCopySetting.Text = "Paramètres de copie"
'
'LblCopySettingTo
'
Me.LblCopySettingTo.AutoSize = True
Me.LblCopySettingTo.Location = New System.Drawing.Point(20, 58)
Me.LblCopySettingTo.Name = "LblCopySettingTo"
Me.LblCopySettingTo.Size = New System.Drawing.Size(34, 13)
Me.LblCopySettingTo.TabIndex = 12
Me.LblCopySettingTo.Text = "Vers :"
'
'LblCopySettingFrom
'
Me.LblCopySettingFrom.AutoSize = True
Me.LblCopySettingFrom.Location = New System.Drawing.Point(27, 29)
Me.LblCopySettingFrom.Name = "LblCopySettingFrom"
Me.LblCopySettingFrom.Size = New System.Drawing.Size(27, 13)
Me.LblCopySettingFrom.TabIndex = 11
Me.LblCopySettingFrom.Text = "De :"
'
'TxbCopySettingTo
'
Me.TxbCopySettingTo.BackColor = System.Drawing.Color.White
Me.TxbCopySettingTo.Location = New System.Drawing.Point(65, 55)
Me.TxbCopySettingTo.Name = "TxbCopySettingTo"
Me.TxbCopySettingTo.ReadOnly = True
Me.TxbCopySettingTo.Size = New System.Drawing.Size(375, 20)
Me.TxbCopySettingTo.TabIndex = 10
'
'BtnCopySettingTo
'
Me.BtnCopySettingTo.Location = New System.Drawing.Point(446, 53)
Me.BtnCopySettingTo.Name = "BtnCopySettingTo"
Me.BtnCopySettingTo.Size = New System.Drawing.Size(75, 23)
Me.BtnCopySettingTo.TabIndex = 9
Me.BtnCopySettingTo.Text = "..."
Me.BtnCopySettingTo.UseVisualStyleBackColor = True
'
'TxbCopySettingFrom
'
Me.TxbCopySettingFrom.BackColor = System.Drawing.Color.White
Me.TxbCopySettingFrom.Location = New System.Drawing.Point(65, 26)
Me.TxbCopySettingFrom.Name = "TxbCopySettingFrom"
Me.TxbCopySettingFrom.ReadOnly = True
Me.TxbCopySettingFrom.Size = New System.Drawing.Size(375, 20)
Me.TxbCopySettingFrom.TabIndex = 8
'
'BtnCopySettingFrom
'
Me.BtnCopySettingFrom.Location = New System.Drawing.Point(446, 24)
Me.BtnCopySettingFrom.Name = "BtnCopySettingFrom"
Me.BtnCopySettingFrom.Size = New System.Drawing.Size(75, 23)
Me.BtnCopySettingFrom.TabIndex = 7
Me.BtnCopySettingFrom.Text = "..."
Me.BtnCopySettingFrom.UseVisualStyleBackColor = True
'
'GbxCopyProgress
'
Me.GbxCopyProgress.Controls.Add(Me.LblCopyProgress)
Me.GbxCopyProgress.Controls.Add(Me.PgbLblCopyProgress)
Me.GbxCopyProgress.Location = New System.Drawing.Point(12, 147)
Me.GbxCopyProgress.Name = "GbxCopyProgress"
Me.GbxCopyProgress.Size = New System.Drawing.Size(541, 76)
Me.GbxCopyProgress.TabIndex = 9
Me.GbxCopyProgress.TabStop = False
Me.GbxCopyProgress.Text = "Progression de la copie"
'
'LblCopyProgress
'
Me.LblCopyProgress.Location = New System.Drawing.Point(23, 15)
Me.LblCopyProgress.Name = "LblCopyProgress"
Me.LblCopyProgress.Size = New System.Drawing.Size(498, 20)
Me.LblCopyProgress.TabIndex = 9
Me.LblCopyProgress.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
'
'PgbLblCopyProgress
'
Me.PgbLblCopyProgress.Location = New System.Drawing.Point(23, 38)
Me.PgbLblCopyProgress.Name = "PgbLblCopyProgress"
Me.PgbLblCopyProgress.Size = New System.Drawing.Size(498, 23)
Me.PgbLblCopyProgress.TabIndex = 8
'
'BgwCopying
'
Me.BgwCopying.WorkerReportsProgress = True
Me.BgwCopying.WorkerSupportsCancellation = True
'
'BtnCopy
'
Me.BtnCopy.Location = New System.Drawing.Point(35, 110)
Me.BtnCopy.Name = "BtnCopy"
Me.BtnCopy.Size = New System.Drawing.Size(498, 31)
Me.BtnCopy.TabIndex = 10
Me.BtnCopy.Text = "Lancer la copie"
Me.BtnCopy.UseVisualStyleBackColor = True
'
'Frm_Main
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(565, 235)
Me.Controls.Add(Me.BtnCopy)
Me.Controls.Add(Me.GbxCopyProgress)
Me.Controls.Add(Me.GbxCopySetting)
Me.Name = "Form1"
Me.Text = "CopyProgress Project"
Me.GbxCopySetting.ResumeLayout(False)
Me.GbxCopySetting.PerformLayout()
Me.GbxCopyProgress.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
Friend WithEvents GbxCopySetting As System.Windows.Forms.GroupBox
Friend WithEvents LblCopySettingTo As System.Windows.Forms.Label
Friend WithEvents LblCopySettingFrom As System.Windows.Forms.Label
Friend WithEvents TxbCopySettingTo As System.Windows.Forms.TextBox
Friend WithEvents BtnCopySettingTo As System.Windows.Forms.Button
Friend WithEvents TxbCopySettingFrom As System.Windows.Forms.TextBox
Friend WithEvents BtnCopySettingFrom As System.Windows.Forms.Button
Friend WithEvents GbxCopyProgress As System.Windows.Forms.GroupBox
Friend WithEvents LblCopyProgress As System.Windows.Forms.Label
Friend WithEvents PgbLblCopyProgress As System.Windows.Forms.ProgressBar
Friend WithEvents BgwCopying As System.ComponentModel.BackgroundWorker
Friend WithEvents BtnCopy As System.Windows.Forms.Button
#End Region
#Region " MAIN PROGRAM "
#Region " COPY SETTINGS "
Private _fbd As FolderBrowserDialog = Nothing
Private Sub BtnCopySettingTo_Click(sender As Object, e As EventArgs) Handles BtnCopySettingTo.Click
_fbd = New FolderBrowserDialog
With _fbd
.Description = "Sélectionnez le répertoire de destination"
If .ShowDialog() = DialogResult.OK Then
TxbCopySettingTo.Text = .SelectedPath
End If
End With
End Sub
Private Sub BtnCopySettingFrom_Click(sender As Object, e As EventArgs) Handles BtnCopySettingFrom.Click
_fbd = New FolderBrowserDialog
With _fbd
.Description = "Sélectionnez le répertoire des fichiers à copier"
If .ShowDialog() = DialogResult.OK Then
TxbCopySettingFrom.Text = .SelectedPath
End If
End With
End Sub
#End Region
#Region " COPY START "
Private Sub BtnCopy_Click(sender As Object, e As EventArgs) Handles BtnCopy.Click
If Directory.Exists(TxbCopySettingFrom.Text) AndAlso Directory.Exists(TxbCopySettingTo.Text) Then
'On vérifie si le même thread Async n'est pas déjà en cours d'exécution avant de le lancer
If Not BgwCopying.IsBusy Then
BgwCopying.RunWorkerAsync()
End If
End If
End Sub
#End Region
#Region " COPY THREAD "
Private Sub BgwCopying_DoWork(sender As Object, e As System.ComponentModel.DoWorkEventArgs) Handles BgwCopying.DoWork
'MAJ de la partie UI = On appelle l'event ProgressChanged pour sortir du MainThread afin d'éviter de retourner des erreurs du type :
'"Opération inter-threads non valide*: le contrôle '' a fait l'objet d'un accès à partir d'un thread autre que celui sur lequel il a été créé." !
'La notion de collecte est importante car s'il y a un gros volume de fichiers alors on préfèrera montrer à l'utilisateur qu'une tache de collecte est en cours !
BgwCopying.ReportProgress(150, "Collecte des fichiers ...")
'Collecte des fichiers depuis le répertoire sélectionné avec un filtre autorisant toutes extension.
' ATTENTION : si tu veux copier des arborescences complètes c'est à toi de le prévoir en implémentant une sorte de fonction XCOPY !
Dim files As String() = Directory.GetFiles(TxbCopySettingFrom.Text, "*.*", SearchOption.AllDirectories)
Dim fCount% = files.Count
If fCount <> 0 Then
For i% = 0 To fCount - 1
If Not File.Exists(TxbCopySettingTo.Text & "\" & New FileInfo(files(i)).Name) Then
'MAJ de la partie UI afin de sortir du mainThread pour modifier la valeur de la PgBar et du label qui contiendra le fichier en cours de copie (très utile pour les fichiers volumineux).
BgwCopying.ReportProgress(i * 100 / fCount, "Copie de : " & New FileInfo(files(i)).Name)
'ATTENTION : prévoir ici l'opération d'annulation
File.Copy(files(i), TxbCopySettingTo.Text & "\" & New FileInfo(files(i)).Name)
End If
Next
End If
'ATTENTION : Prendre en compte également que si tu souhaites englober ton coprs d'instruction dans un Try...Catch qu'il existe des subtilités pour les retours d'erreurs !!!!!
End Sub
Private Sub BgwCopying_ProgressChanged(sender As Object, e As System.ComponentModel.ProgressChangedEventArgs) Handles BgwCopying.ProgressChanged
'La valeur 150 corresponds à la MAJ du label afin d'afficher qu'une collecte est en cours
If e.ProgressPercentage = 150 Then
LblCopyProgress.Text = e.UserState.ToString
Else
'MAJ du label d'infos de copie et de la pgbar.
'Ici la valeur e.progressPercentage prends la valeur de 0 à 100 = le pourcentage de progression
LblCopyProgress.Text = e.UserState.ToString
PgbLblCopyProgress.Value = e.ProgressPercentage
End If
End Sub
Private Sub BgwCopying_RunWorkerCompleted(sender As Object, e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BgwCopying.RunWorkerCompleted
'Fin de la copie
'ATTENTION : prévoir ici la sortie du mainthread qui a retourné des erreur de copie ou bien une annulation
MessageBox.Show("Copie terminée", "Complet", MessageBoxButtons.OK, MessageBoxIcon.Information)
LblCopyProgress.Text = String.Empty
PgbLblCopyProgress.Value = 0
End Sub
#End Region
#End Region
End Class |
Partager