Edit

Share via


Directory.Move(String, String) Method

Definition

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

public:
 static void Move(System::String ^ sourceDirName, System::String ^ destDirName);
public static void Move (string sourceDirName, string destDirName);
static member Move : string * string -> unit
Public Shared Sub Move (sourceDirName As String, destDirName As String)

Parameters

sourceDirName
String

The path of the file or directory to move.

destDirName
String

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

Exceptions

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

-or-

destDirName already exists. See the note in the Remarks section.

-or-

The sourceDirName and destDirName parameters refer to the same file or directory.

-or-

The directory or a file within it is being used by another process.

The caller does not have the required permission.

.NET Framework and .NET Core versions older than 2.1: sourceDirName or destDirName is a zero-length string, contains only white space, or contains one or more invalid characters. You can query for invalid characters with the GetInvalidPathChars() method.

sourceDirName or destDirName is null.

The specified path, file name, or both exceed the system-defined maximum length.

The path specified by sourceDirName is invalid (for example, it is on an unmapped drive).

Examples

The following example demonstrates how to move a directory and all its files to a new directory. The original directory no longer exists after it has been moved.

using System;
using System.IO;

namespace ConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceDirectory = @"C:\source";
            string destinationDirectory = @"C:\destination";

            try
            {
                Directory.Move(sourceDirectory, destinationDirectory);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }
    }
}
open System.IO

let sourceDirectory = @"C:\source"
let destinationDirectory = @"C:\destination"

try
    Directory.Move(sourceDirectory, destinationDirectory)
with e ->
    printfn $"{e.Message}"
Imports System.IO

Module Module1

    Sub Main()
        Dim sourceDirectory As String = "C:\source"
        Dim destinationDirectory As String = "C:\destination"

        Try
            Directory.Move(sourceDirectory, destinationDirectory)
        Catch e As Exception
            Console.WriteLine(e.Message)
        End Try

    End Sub

End Module

Remarks

This method creates a new directory with the name specified by destDirName and moves the contents of sourceDirName, including files and directories, to the newly created destination directory. It then deletes the sourceDirName directory.

If you try to move a directory to a directory that already exists, an IOException will occur.

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 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.

Note

Starting with .NET Core 3.0, the Move method throws an IOException in all platforms when the destDirName already exists. In .NET Core 2.2 and previous versions, the exception was only thrown on Windows, and other platforms could either fail or overwrite the destDirName. See C++ rename.

Applies to

See also