System.IO.Directory.Move Method

Moves a file or a directory and its contents to a new location.

Syntax

public static void Move (string sourceDirName, string destDirName)

Parameters

sourceDirName
The path of the file or directory to move.
destDirName
The path to the new location for sourceDirName. If sourceDirName is a file, then destDirName must also be a file name.

Exceptions

TypeReason
ArgumentException sourceDirName or destDirName is a zero-length string, contains only white space, or contains implementation-specific invalid characters.
ArgumentNullException sourceDirName or destDirName is null.
System.Security.SecurityExceptionThe caller does not have the required permission.
System.IO.IOException

An attempt was made to move a directory to a different volume,

-or-

destDirName already exists.

-or-

sourceDirName and destDirName refer to the same file or directory.

System.IO.DirectoryNotFoundException sourceDirName was not found.
System.IO.PathTooLongExceptionThe length or absolute path information for sourceDirName or destDirName exceeds the system-defined maximum length.
UnauthorizedAccessExceptionThe caller does not have the required permission.

Remarks

This method creates a new directory with the name specified by destDirName and moves the contents of sourceDirName to the newly created destination directory. If you try to move a directory to a directory that already exists, an System.IO.IOException will occur. For example, an exception will occur if you try to move c:\mydir to c:\public, and c:\public already exists. Alternatively, you could specify "c:\\public\\mydir" as the destDirName parameter, provided that "mydir" does not exist under "c:\\public", or specify a new directory name such as "c:\\newdir".

The sourceDirName and destDirName arguments are permitted to specify relative or absolute path information. Relative path information is interpreted as relative to the current working directory. To obtain the current working directory, see Directory.GetCurrentDirectory.

Trailing spaces are removed from the end of the path parameters before moving the directory.

For a list of common I/O tasks, see Common I/O Tasks.

Permissions

TypeReason
System.Security.Permissions.FileIOPermissionRequires permission to read from sourceDirName, and write to sourceDirName and destDirName. See System.Security.Permissions.FileIOPermissionAccess.Read, System.Security.Permissions.FileIOPermissionAccess.Write.

Requirements

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0