Copia ricorsiva di File e Cartelle in VB.Net

by davide 20. December 2008 13:19

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


Currently rated 3.3 by 3 people

  • Currently 3.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , ,

vb.net | Tricks & Tips

Comments

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by Esia Software 1.4.0.0

Informazioni sull'autore

Sono un cittadini di Trieste, ho 33 anni e faccio lo sviluppatore nella mia azienda che si chiama Esia Software.

Commenti recenti

Comment RSS

Post recenti

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar

Info legali

Le opinioni espresse in questo blog sono strettamente personali e ogni persona è responsabile dei commenti che inserisce.

RecentComments

Comment RSS