Ultimi Articoli
Visual Basic
Connessione a Database Microsoft SQL
Visual Basic
I controlli ListBox e ComboBox
ASP
Verificare l'esistenza di un file sul Server
Visual Basic
Informazioni aggiuntive sugli ActiveX
Visual Basic
Installare un file .inf

Visual Basic .NET

SORGENTI - OPERAZIONI SUI FILE - COPIA DI UNA DIRECTORY IN UN'ALTRA POSIZIONE CON OPZIONE DI SOVRASCRITTURA.



Sub CopyDirectory(ByVal sourcePath As String, ByVal destPath As String, Optional ByVal overwrite As Boolean = False)
Dim sourceDir As DirectoryInfo = New DirectoryInfo(sourcePath)
Dim destDir As DirectoryInfo = New DirectoryInfo(destPath)

' the source directory must exist, otherwise throw an exception
If sourceDir.Exists Then
' if destination subDir's parent subDir does not exist throw an exception
If Not destDir.Parent.Exists Then
Throw New DirectoryNotFoundException ("Destination directory does not exist: " + destDir.Parent.FullName)
End If

If Not destDir.Exists Then
destDir.Create()
End If

' copy all the files of the current directory
Dim childFile As FileInfo
For Each childFile In sourceDir.GetFiles()
If overwrite Then
childFile.CopyTo(Path.Combine(destDir.FullName, childFile.Name), True)
Else
' if overwrite = false, copy the file only if it does not exist
' this is done to avoid an IOException if a file already exists
' this way the other files can be copied anyway...
If Not File.Exists(Path.Combine(destDir.FullName, childFile.Name)) Then
childFile.CopyTo(Path.Combine(destDir.FullName, childFile.Name), False)
End If
End If
Next

' copy all the sub-directories by recursively calling this same routine
Dim subDir As DirectoryInfo
For Each subDir In sourceDir.GetDirectories()
CopyDirectory(subDir.FullName, Path.Combine(destDir.FullName, subDir.Name), overwrite)
Next
Else
Throw New DirectoryNotFoundException("Source directory does not exist: " + sourceDir.FullName)
End If
End Sub