My.Computer.FileSystem.MoveDirectory Method 

Moves a directory from one location to another.

' Usage
My.Computer.FileSystem.MoveDirectory(sourceDirectoryName ,destinationDirectoryName)
My.Computer.FileSystem.MoveDirectory(sourceDirectoryName ,destinationDirectoryName ,overwrite)
My.Computer.FileSystem.MoveDirectory(sourceDirectoryName ,destinationDirectoryName ,showUI)
My.Computer.FileSystem.MoveDirectory(sourceDirectoryName ,destinationDirectoryName ,showUI ,onUserCancel)

' Declaration
Public Sub MoveDirectory( _
   ByVal sourceDirectoryName As String, _
   ByVal destinationDirectoryName As String _
)
' -or-
Public Sub MoveDirectory( _
   ByVal sourceDirectoryName As String, _
   ByVal destinationDirectoryName As String, _
   ByVal overwrite As Boolean _
)
' -or-
Public Sub MoveDirectory( _
   ByVal sourceDirectoryName As String, _
   ByVal destinationDirectoryName As String, _
   ByVal showUI As UIOption _
)
' -or-
Public Sub MoveDirectory( _
   ByVal sourceDirectoryName As String, _
   ByVal destinationDirectoryName As String, _
   ByVal showUI As UIOption, _
   ByVal onUserCancel As UICancelOption _
)

Parameters

  • sourceDirectoryName
    String. Path of the directory to be moved. Required.
  • destinationDirectoryName
    String. Path of the directory to which the source directory is being moved. Required.
  • overwrite
    Boolean. Specifies whether existing directories should be overwritten. Default is False. Required.
  • showUI
    UIOption. Specifies whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs. Required.
  • onUserCancel
    UICancelOption Enumeration. Specifies whether or not an exception is thrown when the user cancels the operation. Default is UICancelOption.ThrowException. Required.

Exceptions

The following conditions may cause an exception:

  • The path is a zero-length string, contains only white space, contains invalid characters, or 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).

  • onUserCancel is set to ThrowException and a subdirectory of the file cannot be copied (IOException).

  • The operation is cyclic (InvalidOperationException).

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

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

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

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

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

Remarks

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

Tasks

The following table lists an example of a task involving the My.Computer.FileSystem.MoveDirectory method.

To See

Move a directory

How to: Move a Directory in Visual Basic

Example

This example movesDirectory1inside Directory2.

My.Computer.FileSystem.MoveDirectory("C:\Directory1", "C:\Directory2")

This example movesDirectory1inside Directory2, overwriting the directory if it already exists.

My.Computer.FileSystem.MoveDirectory("C:\Directory1", "C:\Directory2", _
True)

Requirements

Namespace: Microsoft.VisualBasic.MyServices

Class: FileSystemProxy (provides access to FileSystem)

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

Availability by Project Type

Project type Available

Windows Application

Yes

Class Library

Yes

Console Application

Yes

Windows Control Library

Yes

Web Control Library

Yes

Windows Service

Yes

Web Site

Yes

Permissions

The following permissions may be necessary:

Permission Description

FileIOPermission

Controls the ability to access files and folders. Associated enumeration: Unrestricted.

UIPermission

Controls the permissions related to user interfaces and the clipboard. Associated enumeration: SafeSubWindows.

For more information, see Code Access Security and Requesting Permissions.

See Also

Reference

My.Computer.FileSystem Object
UIOption Enumeration
UICancelOption Enumeration
My.Computer.FileSystem.MoveFile Method
My.Computer.FileSystem.CopyDirectory Method

Other Resources

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