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
|
'Form principal identique au premier deja poste
'1 Button
'2 FolderBrowserDialog
Imports System.IO
Public Class frmCopyFoldersPerso
'dossier source
Private dirNameSource As String
'nouveau dossier cible (à creer FolderBrowserDialog2 s'il n'existe pas)
Private dirNameDest As String
Private Sub SelectCopyFolders_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
If Len(dirNameSource) = 0 Then Exit Sub
End If
' Show the FolderBrowserDialog 2.
result = FolderBrowserDialog2.ShowDialog()
If (result = DialogResult.OK) Then
dirNameDest = FolderBrowserDialog2.SelectedPath
If Len(dirNameDest) = 0 Then Exit Sub
End If
'si True tous les sous-dossiers sont copies
Call DirectoryCopyExample.DirectoryCopy(dirNameSource, dirNameDest, True)
End Sub
End Class
'1-Classe DirectoryCopyExample
'- copie "recursivement" le dossier source et ses sous dossiers
'L'utilisateur peut spécifier si les sous-répertoires doivent également être copiés.
'-si copySubDirs=True, la méthode les copie de manière récursive
'en s'appelant sur chaque sous-répertoire suivant
'jusqu'à ce que toutes les copies soient effectuées.
Imports System
Imports System.IO
Public Class DirectoryCopyExample
Public Shared Sub DirectoryCopy( _
ByVal sourceDirName As String, _
ByVal destDirName As String, _
ByVal copySubDirs As Boolean)
Dim dir As DirectoryInfo = New DirectoryInfo(sourceDirName)
Dim dirs As DirectoryInfo() = dir.GetDirectories()
' If the source directory does not exist, throw an exception.
If Not dir.Exists 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
' Get the file contents of the directory to copy.
Dim files As FileInfo() = dir.GetFiles()
'Initialise frmProgress & Affiche la copie en pourcent
Dim frm As frmProgress = New frmProgress
frm.ProgressBar1.Minimum = 1
frm.ProgressBar1.Maximum = 100
frm.ProgressBar1.Value = 1
frm.ProgressBar1.Step = 1
frm.Show()
frm.TopMost = True
Dim strDisplay As String = String.Empty
Dim strPerCent As String = String.Empty
For Each file In files
' Create the path to the new copy of the file.
Dim temppath As String = Path.Combine(destDirName, file.Name)
' Copy the file.
file.CopyTo(temppath, True)
'Update ProgressBar1
frm.ProgressBar1.PerformStep()
'Display
frm.lblPerCent.Text = CInt(100 * frm.ProgressBar1.Value / files.Length)
frm.lblPerCent.Refresh()
frm.lblDirectoryFiles.Text = file.DirectoryName & file.Name
frm.lblDirectoryFiles.Refresh()
Next file
frm.Close()
' If copySubDirs is true, copy the subdirectories.
' la methode DirectoryCopy s'appelle elle-meme "recursivement"
' pour faire la copie des sous-dossier ..et les afficher......ainsi de suite
If copySubDirs Then
For Each subdir In dirs
' Create the subdirectory.
Dim temppath As String = _
Path.Combine(destDirName, subdir.Name)
' Copy the subdirectories.
DirectoryCopy(subdir.FullName, temppath, copySubDirs)
Next subdir
End If
End Sub
End Class
'un simple frmProgress charge d'afficher:
'- 1 progressBar
'- 2 labels d'information
Public Class frmProgress
End Class |
Partager