HOW TO >VB.NET
Copiare un intera Directory e relative sottocartelle. Inserire sul form una Label (LbProgressivo) Metodo 1 : copia con stato di avanzamento

  1. Imports System
  2. Imports System.IO
  3. Public Class Form1
  4.     Public ProgFiles, TotFiles As Integer
  5.  
  6.     Private Sub Bt1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Bt1.Click
  7.         Dim DirectoryOrigine As String = "c:\test"
  8.         Dim DirectoryDestinazione As String = "c:\testnew"
  9.         Try
  10.             'Conto i files nella directory origine per visualizzare lo stato di avanzamento
  11.             Dim dDir1 As New DirectoryInfo(DirectoryOrigine)
  12.             TotFiles = dDir1.GetFiles("*.*", SearchOption.AllDirectories).Length
  13.             ProgFiles = 0
  14.             'Effettuo la copia della directory
  15.             CopyDirectory(DirectoryOrigine, DirectoryDestinazione, LbProgressivo)
  16.             MessageBox.Show("Operazione completata")
  17.             'Ripristino lo stato precedente
  18.             '(la riga seguente e' da togliere nell'utilizzo della routine)
  19.             Directory.Delete(DirectoryDestinazione, True)
  20.         Catch ex As Exception
  21.             MessageBox.Show(ex.Message & Environment.NewLine & ex.StackTrace)
  22.         End Try
  23.     End Sub
  24.  
  25.     Sub CopyDirectory(ByVal Origine As String, ByVal Destinazione As String, ByVal Lb As Label)
  26.         Dim Lb1 As Label = Lb
  27.         Dim CartellaCorrente As DirectoryInfo = New DirectoryInfo(Origine)
  28.         Dim Archivo As FileInfo
  29.         Dim Cartella As DirectoryInfo
  30.  
  31.         For Each Archivo In CartellaCorrente.GetFiles()
  32.             If Not Directory.Exists(Destinazione) Then Directory.CreateDirectory(Destinazione)
  33.             Try
  34.                 Archivo.CopyTo(Path.Combine(Destinazione, Archivo.Name))
  35.                 ProgFiles += 1
  36.                 Lb1.Text = "In fase di copia " & ProgFiles & " file di "
  37.                 Lb1.Text += TotFiles & " ( " & Archivo.Name & ")"
  38.                 Application.DoEvents()
  39.             Catch ex As Exception
  40.                 'Errore in copia file...
  41.                 MessageBox.Show(ex.Message & Environment.NewLine & ex.StackTrace)
  42.             End Try
  43.             Application.DoEvents()
  44.         Next
  45.  
  46.         For Each Cartella In CartellaCorrente.GetDirectories()
  47.             Dim subDirectory As String = Path.Combine(Destinazione, Cartella.Name)
  48.             Try
  49.                 Directory.CreateDirectory(subDirectory)
  50.             Catch ex As Exception
  51.                 'Errore in creazione directory...
  52.                 MessageBox.Show(ex.Message & Environment.NewLine & ex.StackTrace)
  53.             End Try
  54.  
  55.             CopyDirectory(Cartella.FullName, subDirectory, Lb1)
  56.         Next
  57.     End Sub
  58.  
  59. End Class