Export (0) Print
Expand All

Directory.Move Method

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

Namespace:  System.IO
Assembly:  mscorlib (in mscorlib.dll)

public static void Move(
	string sourceDirName,
	string destDirName
)

Parameters

sourceDirName
Type: System.String

The path of the file or directory to move.

destDirName
Type: System.String

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

ExceptionCondition
IOException

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

-or-

destDirName already exists.

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

UnauthorizedAccessException

The caller does not have the required permission.

ArgumentException

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.

ArgumentNullException

sourceDirName or destDirName is null.

PathTooLongException

The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters and file names must be less than 260 characters.

DirectoryNotFoundException

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

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

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);
            }
        }
    }
}

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft