File: Add CopyDirectory method
This commit is contained in:
parent
4eae3d6e80
commit
ebf53ea108
@ -206,6 +206,36 @@ Public Class File
|
||||
IO.File.Move(FilePath, Path.Combine(Directory, NewFileName))
|
||||
End Sub
|
||||
|
||||
''' <summary>
|
||||
''' Copied from https://docs.microsoft.com/en-us/dotnet/standard/io/how-to-copy-directories
|
||||
''' </summary>
|
||||
''' <param name="SourceDirName"></param>
|
||||
''' <param name="DestDirName"></param>
|
||||
''' <param name="CopySubDirs"></param>
|
||||
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
|
||||
|
||||
''' <summary>
|
||||
''' Tries to create a directory and returns its path.
|
||||
''' Returns a temp path if `DirectoryPath` can not be created or written to.
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user