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
|
'Form principal unique & identique au premier deja poste(pas de class ni d'autre forme)
'3 Button
'2 FolderBrowserDialog
'1 BackGroundWorker
'1 progressBar
'2 labels
Imports System.Windows
Imports System.ComponentModel
Imports System.Threading
Imports System.IO
Public Class frmBackGroundWorker
'Total de fichiers à copier
Private totalFichierPourCopie As Integer
'Total de fichiers copies
Private nombreFichiersCopies As Integer
'dossier source
Private dirNameSource As String
'nouveau dossier cible (à creer FolderBrowserDialog2 s'il n'existe pas)
Private dirNameDest As String
Private Sub btnSelectCopyFolders_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelectCopyFolders.Click
dirNameSource = String.Empty
dirNameDest = String.Empty
Dim result As DialogResult
' Show the FolderBrowserDialog 1.
result = FolderBrowserDialog1.ShowDialog()
If (result = DialogResult.OK) Then
dirNameSource = FolderBrowserDialog1.SelectedPath
End If
' Show the FolderBrowserDialog 2.
result = FolderBrowserDialog2.ShowDialog()
If (result = DialogResult.OK) Then
dirNameDest = FolderBrowserDialog2.SelectedPath
End If
If Len(dirNameSource) = 0 Or Len(dirNameDest) = 0 Then Exit Sub
' Calcul Total Fichiers à copier.
totalFichierPourCopie = 0
totalFichierPourCopie = FilesTotal(dirNameSource, totalFichierPourCopie)
Me.lblTotalFichierPourCopie.Text = totalFichierPourCopie.ToString
End Sub
Private Sub btnStartAsync_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStartAsync.Click
If Len(Me.dirNameSource) = 0 Or Len(Me.dirNameDest) = 0 Then Exit Sub
If Me.totalFichierPourCopie = 0 Then Exit Sub
' Reset du label resultat.
Me.resultLabel.Text = String.Empty
' Desactive start button jusqu'a fin operation
' asynchrone.
Me.btnStartAsync.Enabled = False
' Active Cancel button sur duree operation asynchrone.
Me.btnCancelAsync.Enabled = True
' ProgressBar1
Me.ProgressBar1.Minimum = 0
Me.ProgressBar1.Maximum = 100
Me.ProgressBar1.Value = 1
Me.ProgressBar1.Visible = True
Me.ProgressBar1.Enabled = True
'Aucun fichier copie
Me.nombreFichiersCopies = 0
' Initialize BackgroudWorker.
Me.Worker.WorkerReportsProgress = True
Me.Worker.WorkerSupportsCancellation = True
' Demarre worker.
Me.Worker.RunWorkerAsync()
End Sub
'Interrompt BackgroudWorker
Private Sub btnCancelAsync_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelAsync.Click
' Interrompt operation asynchrone.
Me.Worker.CancelAsync()
' Desactive Cancel button.
btnCancelAsync.Enabled = False
End Sub
Private Sub Worker_DoWork(ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles Worker.DoWork
Call DirectoryCopy(dirNameSource, dirNameDest, Worker, e)
End Sub
Private Sub Worker_ProgressChanged(ByVal sender As Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles Worker.ProgressChanged
Me.ProgressBar1.Value = e.ProgressPercentage
End Sub
Private Sub Worker_RunWorkerCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles Worker.RunWorkerCompleted
' handle an exception was thrown.
If (e.Error IsNot Nothing) Then
MessageBox.Show(e.Error.Message)
ElseIf e.Cancelled Then
' handle the case where the user canceled the
' operation.
resultLabel.Text = "Canceled"
Else
' handle the case where the operation succeeded.
resultLabel.Text = e.Result.ToString()
End If
' Active Start button.
btnStartAsync.Enabled = True
' Desactive Cancel button.
btnCancelAsync.Enabled = False
End Sub
'Fonction copie folders
Private Sub DirectoryCopy(ByVal sourceDirName As String, ByVal destDirName As String, ByVal worker As BackgroundWorker, ByVal e As System.ComponentModel.DoWorkEventArgs)
' If the source directory does not exist, throw an exception.
If Not Directory.Exists(sourceDirName) Then
Throw New DirectoryNotFoundException( _
"Source directory does not exist or could not be found: " _
+ sourceDirName)
End If
' If the destination directory does not exist, create it.
If Not Directory.Exists(destDirName) Then
Directory.CreateDirectory(destDirName)
End If
Dim dir As DirectoryInfo = New DirectoryInfo(sourceDirName)
Dim files() As FileInfo = dir.GetFiles()
For Each file As FileInfo In files
' Create the path to the new copy of the file.
Dim temppath As String = Path.Combine(destDirName, file.Name)
file.CopyTo(temppath, True)
nombreFichiersCopies = nombreFichiersCopies + 1
Dim percentageDone As Double = CType(nombreFichiersCopies / totalFichierPourCopie, Double) * 100.0
worker.ReportProgress(CType(percentageDone, Integer))
Next
Dim dirs As DirectoryInfo() = dir.GetDirectories()
For Each subdir As DirectoryInfo In dirs
' Create the subdirectory.
Dim temppath As String = _
Path.Combine(destDirName, subdir.Name)
' Copy the subdirectories.
DirectoryCopy(subdir.FullName, temppath, worker, e)
Next
e.Result = nombreFichiersCopies.ToString
End Sub
'Fonction Total Fichiers à copier
Private Function FilesTotal(ByVal sourceDirName As String, ByVal totalFile As Integer) As Integer
Dim dir As DirectoryInfo = New DirectoryInfo(sourceDirName)
Dim dirs As DirectoryInfo() = dir.GetDirectories()
Dim files As FileInfo() = dir.GetFiles()
For Each File In files
' Compute total files for subdir
totalFile = totalFile + 1
Next File
'appel recursif sur FilesTotal pour chaque subdir
For Each subdir In dirs
sourceDirName = subdir.FullName
totalFile = FilesTotal(sourceDirName, totalFile)
Next subdir
Return totalFile
End Function
End Class |
Partager