We recommend using Visual Studio 2017
This documentation is archived and is not being maintained.

How to: Move the Contents of a Directory in Visual Basic

Use the GetFiles method to get the list of files in a folder, and use the MoveFile method to move files between directories.

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(
        Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories, "*.*")
    For Each foundFile In fileList
            "C:\StorageDir\" & My.Computer.FileSystem.GetFileInfo(foundFile).Name)

The following conditions may cause an exception: