From ebf53ea108758d11aa98011ff4c5c323795346bd Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Thu, 17 Dec 2020 13:36:29 +0100 Subject: [PATCH] File: Add CopyDirectory method --- Modules.Filesystem/File.vb | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/Modules.Filesystem/File.vb b/Modules.Filesystem/File.vb index af4dae4d..24d617e1 100644 --- a/Modules.Filesystem/File.vb +++ b/Modules.Filesystem/File.vb @@ -206,6 +206,36 @@ Public Class File IO.File.Move(FilePath, Path.Combine(Directory, NewFileName)) End Sub + ''' + ''' Copied from https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories + ''' + ''' + ''' + ''' + Public Sub CopyDirectory(ByVal SourceDirName As String, ByVal DestDirName As String, ByVal CopySubDirs As Boolean) + Dim oDirectory As DirectoryInfo = New DirectoryInfo(SourceDirName) + + If Not oDirectory.Exists Then + Throw New DirectoryNotFoundException("Source directory does not exist or could not be found: " & SourceDirName) + End If + + Dim oDirectories As DirectoryInfo() = oDirectory.GetDirectories() + Directory.CreateDirectory(DestDirName) + Dim oFiles As FileInfo() = oDirectory.GetFiles() + + For Each oFile As FileInfo In oFiles + Dim tempPath As String = Path.Combine(DestDirName, oFile.Name) + oFile.CopyTo(tempPath, False) + Next + + If CopySubDirs Then + For Each oSubDirectory As DirectoryInfo In oDirectories + Dim oTempPath As String = Path.Combine(DestDirName, oSubDirectory.Name) + CopyDirectory(oSubDirectory.FullName, oTempPath, CopySubDirs) + Next + End If + End Sub + ''' ''' Tries to create a directory and returns its path. ''' Returns a temp path if `DirectoryPath` can not be created or written to.