Community
Hi
I looking way to make copy of folder with all it files and subfolders. For single files we can use System.IO.File.Copy. How about folders?
Solved! Go to Solution.
Solved by Ralf_Krieg. Go to Solution.
Try this post on StackOverflow. It is easy to translate to VB.NET when needed.
https://stackoverflow.com/a/3822913/12189568
https://stackoverflow.com/questions/58744/copy-the-entire-contents-of-a-directory-in-c-sharp
Hello
AFAIK no one liner You need to copy recursive step by step.
Imports System.IO
Sub Main
RecursiveFolderCopy("Sourcedirectory", "DestinationDirectory", "TRUE/FALSE")
End Sub
Private Sub RecursiveFolderCopy(ByVal sourceDirectory As String, ByVal destinationDirectory As String, ByVal recursive As Boolean)
Dim dir As DirectoryInfo = New DirectoryInfo(sourceDirectory)
Dim dirs() As DirectoryInfo = dir.GetDirectories
If Not dir.Exists Then
Throw New DirectoryNotFoundException(String.Format("Source directory {0} does not exist!", sourceDirectory))
End If
If Not Directory.Exists(destinationDirectory) Then
Directory.CreateDirectory(destinationDirectory)
End If
Dim files() As FileInfo = dir.GetFiles
For Each file As FileInfo In files
Dim temppath As String = System.IO.Path.Combine(destinationDirectory, File.Name)
File.CopyTo(temppath, False)
Next
If Not recursive Then
Return
End If
For Each subdir As DirectoryInfo In dirs
Dim temppath As String = System.IO.Path.Combine(destinationDirectory, subdir.Name)
RecursiveFolderCopy(subdir.FullName, temppath, recursive)
Next
End Sub
Can't find what you're looking for? Ask the community or share your knowledge.