Export (0) Print
Expand All

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:

  • 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 Nothing (ArgumentNullException).

  • The source is invalid (DirectoryNotFoundException).

  • The source is a root directory (IOException).

  • The combined path points to an existing file (IOException).

  • The source path and the target path are the same (IOException).

  • The file already exists and overwrite is set to False (IOException).

  • A subdirectory of file cannot be copied (IOException).

  • The operation is cyclic (InvalidOperationException).

  • A file or directory name in the path contains a colon (:) (NotSupportedException).

  • onUserCancel is set to UICancelOption.ThrowException, and the user cancels the operation (OperationCanceledException).

  • onUserCancel is set to UICancelOption.ThrowException, and the operation cannot be completed (OperationCanceledException).

  • The path exceeds the system-defined maximum length (PathTooLongException).

  • onUserCancel is set to UICancelOption.ThrowException, and the user lacks necessary permissions (SecurityException).

  • The user does not have permission to modify the file (UnauthorizedAccessException).

See Also

Community Additions

ADD
Show:
© 2014 Microsoft