How to: Move a Directory in Visual Basic

The My.Computer.FileSystem.MoveDirectory Method can be used to move directories.

If an attempt is made to move a directory inside a directory that does not exist, the target directory will be created.

If overwrite is True and the destination directory already exists, the new files will be added to the already existing files in the directory. Files in the destination directory will be overwritten if the source directory contains files with the same name.

To move a directory

  • Use the MoveDirectory method to move a directory, specifying the source and target directories. The following example moves Dir1 inside Dir2.

    My.Computer.FileSystem.MoveDirectory("C:\Dir1", "C:\Dir2")
    

To move a directory and overwrite existing directories

  • Use the MoveDirectory method to move a directory, specifying the source and target directories. The following example moves Dir1 inside Dir2, adding its files to the existing files if the destination directory already exists.

    My.Computer.FileSystem.MoveDirectory("C:\Dir1", "C:\Dir2", True)
    

Robust Programming

The following conditions may cause an exception:

See Also

Tasks

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

How to: Copy a Directory to Another Directory in Visual Basic

How to: Rename a Directory in Visual Basic

How to: Parse File Paths in Visual Basic

Other Resources

Creating, Deleting, and Moving Files and Directories in Visual Basic