VB.Net copy a directory to another directory

~OSD~ 2,196 Reputation points
2020-11-30T11:17:09.367+00:00

Hi,

With following code, I am trying to copy a directory (_sPath) with all files and subdirectories to another folder (_dpath).

Dim _sPath() As String = IO.Directory.GetDirectories(IO.Path.Combine("C:\Users", Environment.UserName, "sDirectory"))
Dim _dPath() As String = IO.Directory.GetDirectories(IO.Path.Combine("C:\Users", Environment.UserName, "dDirectory"))
My.Computer.FileSystem.CopyDirectory(_sPath(0), _dPath(0), overwrite:=True)

Error: Index was outside the bound of array.

All I need is to copy a folder with ALL it's contents to another directory as in CMD:
xcopy C:\Users\%UserName%\sFolder C:\Users\%UserName%\dFolder\ /H /E /C /I /Y /Q

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,737 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Castorix31 85,881 Reputation points
    2020-11-30T13:38:49.95+00:00

    > Error: Index was outside the bound of array.

    _sPath() is probably empty (you get folders inside "C:\Users\%UserName%\sDirectory"

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.