How to: Move the Contents of a Directory in Visual Basic
If the target structure does not exist when MoveFile is called, it will be created.
To move the contents of a directory
Use the GetFiles method to get the list of files in the directory, followed by the MoveFile method, supplying the source file and the directory to which to move it. This example moves all the files in the My Documents directory to the directory named StorageDir.
Dim fileList = My.Computer.FileSystem.GetFiles( My.Computer.FileSystem.SpecialDirectories.MyPictures, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.*") For Each foundFile In fileList My.Computer.FileSystem.MoveFile(foundFile, "C:\StorageDir\" & My.Computer.FileSystem.GetFileInfo(foundFile).Name) Next
The following conditions may cause an exception:
The path is not valid for one of the following reasons: it is a zero-length string, it contains only white space, it contains invalid characters, or it is a device path (starts with \\.\) (ArgumentException).
The path is not valid because it is Nothing (ArgumentNullException).
directory does not exist (DirectoryNotFoundException).
directory points to an existing file (IOException).
The path exceeds the system-defined maximum length (PathTooLongException).
A file or directory name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
The user lacks necessary permissions to view the path (SecurityException).
The user lacks necessary permissions (UnauthorizedAccessException).