Export (0) Print
Expand All

DirectoryInfo.MoveTo Method

Moves a DirectoryInfo instance and its contents to a new path.

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

public void MoveTo (
	string destDirName
)
public void MoveTo (
	String destDirName
)
public function MoveTo (
	destDirName : String
)

Parameters

destDirName

The name and path to which to move this directory. The destination cannot be another disk volume or a directory with the identical name. It can be an existing directory to which you want to add this directory as a subdirectory.

Exception typeCondition

ArgumentNullException

destDirName is a null reference (Nothing in Visual Basic).

-or-

The directory being moved and the destination directory have the same name.

ArgumentException

destDirName is an empty string (''").

IOException

An attempt was made to move a directory to a different volume, or destDirName already exists.

SecurityException

The caller does not have the required permission.

This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. You must specify "c:\\public\\mydir" as the destDirName parameter, or specify a new directory name such as "c:\\newdir".

This method permits moving a directory to a read-only directory. The read/write attribute of neither directory is affected.

The following table lists examples of other typical or related I/O tasks.

To do this...

See the example in this topic...

Create a text file.

How to: Write Text to a File

Write to a text file.

How to: Write Text to a File

Read from a text file.

How to: Read Text from a File

Delete a directory.

Directory.Delete

DirectoryInfo.Delete

Create a directory.

CreateDirectory

Directory

Create a subdirectory.

CreateSubdirectory

See the files in a directory.

Name

See the subdirectories of a directory.

GetDirectories

GetDirectories

See all the files and all the subdirectories in a directory.

GetFileSystemInfos

Find the size of a directory.

Directory

Determine if a file exists.

Exists

Determine if a directory exists.

Exists

The following example demonstrates moving a directory.

using System;
using System.IO;

public class MoveToTest 
{
    public static void Main() 
    {

        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.Exists == false)
            di.Create();

        // Create a subdirectory in the directory just created.
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // Move the main directory. Note that the contents move with the directory.
        if (Directory.Exists("NewTempDir") == false)
            di.MoveTo("NewTempDir");

        try 
        {
            // Attempt to delete the subdirectory. Note that because it has been
            // moved, an exception is thrown.
            dis.Delete(true);
        } 
        catch (Exception) 
        {
            // Handle this exception in some way, such as with the following code:
            // Console.WriteLine("That directory does not exist.");
        }

        // Point the DirectoryInfo reference to the new directory.
        //di = new DirectoryInfo("NewTempDir");

        // Delete the directory.
        //di.Delete(true);
    }
}

import System.*;
import System.IO.*;

public class MoveToTest
{
    public static void main(String[] args)
    {
        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.get_Exists() == false) {
            di.Create();
        }

        // Create a subdirectory in the directory just created.
        DirectoryInfo dis = di.CreateSubdirectory("SubDir");

        // Move the main directory. 
        // Note that the contents move with the directory.
        if (Directory.Exists("NewTempDir") == false) {
            di.MoveTo("NewTempDir");
        }

        try {
            // Attempt to delete the subdirectory. 
            // Note that because it has been
            // moved, an exception is thrown.
            dis.Delete(true);
        }
        catch (System.Exception exp) {
            // Handle this exception in some way, such as 
            // with the following code:
            // Console.WriteLine("That directory does not exist.");
        }

        // Point the DirectoryInfo reference to the new directory.
        //di = new DirectoryInfo("NewTempDir");
        // Delete the directory.
        //di.Delete(true);
    } //main
} //MoveToTest 

import System;
import System.IO;

public class MoveToTest {
    public static function Main() {

        // Make a reference to a directory.
        var di : DirectoryInfo = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.Exists == false)
            di.Create();

        // Create a subdirectory in the directory just created.
        var dis : DirectoryInfo = di.CreateSubdirectory("SubDir");

        // Move the main directory. Note that its contents move also.
        if (Directory.Exists("NewTempDir") == false)
            di.MoveTo("NewTempDir");

        try {
            // Attempt to delete the subdirectory. Note that because it has been
            // moved, an exception is thrown.
            dis.Delete(true);
        } catch (Exception) {
            // Handle this exception in some way, as with the following code:
            // Console.WriteLine("That directory does not exist.");
        }

        // Point the DirectoryInfo reference to the new directory.
        //di = new DirectoryInfo("NewTempDir");

        // Delete the directory.
        //di.Delete(true);
    }
}
MoveToTest.Main();

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

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

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft