DirectoryInfo.MoveTo(String) Método

Definición

Mueve una instancia de DirectoryInfo y su contenido a una nueva ruta de acceso.

public:
 void MoveTo(System::String ^ destDirName);
public void MoveTo (string destDirName);
member this.MoveTo : string -> unit
Public Sub MoveTo (destDirName As String)

Parámetros

destDirName
String

Nombre y ruta de acceso a la que se va a mover este directorio. El destino no puede ser otro volumen de disco ni un directorio con el mismo nombre. Puede ser un directorio existente al que desee agregar este directorio como subdirectorio.

Excepciones

destDirName es null.

destDirName es una cadena vacía ("").

Se ha intentado mover un directorio a otro volumen.

o bien

destDirName ya existe.

o bien

No tiene autorización para esta ruta de acceso.

o bien

El directorio que se está moviendo y el directorio de destino tienen el mismo nombre.

El llamador no dispone del permiso requerido.

No se puede encontrar el directorio de destino.

Ejemplos

En el ejemplo siguiente se muestra cómo mover un directorio.

using namespace System;
using namespace System::IO;
int main()
{
   
   // Make a reference to a directory.
   DirectoryInfo^ di = gcnew DirectoryInfo( "TempDir" );
   
   // Create the directory only if it does not already exist.
   if (  !di->Exists )
      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" ) )
      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(S"That directory does not exist.");
   }

   
   // Point the DirectoryInfo reference to the new directory.
   //di = new DirectoryInfo(S"NewTempDir");
   // Delete the directory.
   //di->Delete(true);
}
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);
    }
}
open System.IO

// Make a reference to a directory.
let di = DirectoryInfo "TempDir"

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

// Create a subdirectory in the directory just created.
let dis = di.CreateSubdirectory "SubDir"

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

try
    // Attempt to delete the subdirectory. Note that because it has been
    // moved, an exception is thrown.
    dis.Delete true
with _ ->
    // 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)
Imports System.IO

Public Class MoveToTest

    Public Shared Sub Main()
        ' Make a reference to a directory.
        Dim di As New DirectoryInfo("TempDir")
        ' Create the directory only if it does not already exist.
        If di.Exists = False Then
            di.Create()
        End If

        ' Create a subdirectory in the directory just created.
        Dim dis As DirectoryInfo = di.CreateSubdirectory("SubDir")
        If Directory.Exists("NewTempDir") = False Then
            ' Move the main directory. Note that the contents move with the directory.
            di.MoveTo("NewTempDir")
        End If
        Try
            ' Attempt to delete the subdirectory. Note that because it has been
            ' moved, an exception is thrown.
            dis.Delete(True)
        Catch
            ' 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)        
        End Try

    End Sub
End Class

Comentarios

Este método produce un IOException si, por ejemplo, intenta mover c:\mydir a c:\public y c:\public ya existe. Debe especificar "c:\\public\\mydir" como destDirName parámetro o especificar un nombre de directorio nuevo, como "c:\\newdir".

Este método permite mover un directorio a un directorio de solo lectura. El atributo de lectura y escritura de ninguno de los directorios se ve afectado.

Para obtener una lista de tareas de E/S comunes, consulte Tareas de E/S comunes.

Se aplica a

Consulte también