Sembra banale, ma a volte la semplice copia dei file e delle sottodirectory contenute in una cartella può mettere in crisi un programmatore, proprio per la sua banalità.
Proprio per evitare questo, posto un esempio di copia ricorsiva.
Private Sub Copia(ByVal PathOrigine As String, _
            ByVal PathDestinazione As String)
    Dim Files() As String
    If PathDestinazione.Chars(PathDestinazione.Length - 1) <>
            Path.DirectorySeparatorChar Then
        PathDestinazione += Path.DirectorySeparatorChar
    End If
    If Not Directory.Exists(PathDestinazione) Then
        Directory.CreateDirectory(PathDestinazione)
    End If
    Files = Directory.GetFileSystemEntries(PathOrigine)
    Dim Elemento As String
    For Each Elemento In Files
        'Sotto cartelle
        If Directory.Exists(Elemento) Then
            Copia(Elemento, PathDestinazione +
                Path.GetFileName(Elemento))
            ' File nella cartella
         Else
            File.Copy(Elemento, PathDestinazione +
                Path.GetFileName(Elemento), True)
        End If
    Next Elemento
End Sub
 
 
Nessun commento:
Posta un commento