DirectoryInfo.MoveTo Method
Assembly: mscorlib (in mscorlib.dll)
| Exception type | Condition |
|---|---|
| destDirName is a null reference (Nothing in Visual Basic). -or- The directory being moved and the destination directory have the same name. |
|
| destDirName is an empty string (''"). |
|
| An attempt was made to move a directory to a different volume, or destDirName already exists. |
|
| 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. | |
| Write to a text file. | |
| Read from a text file. | |
| Delete a directory. | |
| Create a directory. | |
| Create a subdirectory. | |
| See the files in a directory. | |
| See the subdirectories of a directory. | |
| See all the files and all the subdirectories in a directory. | |
| Find the size of a directory. | |
| Determine if a file exists. | |
| Determine if a directory 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();
- FileIOPermissionAccess for reading and writing files and directories and for access to the destination directory. Associated enumerations: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write
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.