DirectoryInfo.MoveTo(String) Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Mueve una DirectoryInfo instancia 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 o un directorio con el nombre idéntico. Puede ser un directorio existente al que desea agregar este directorio como subdirectorio.
Excepciones
destDirName es null.
destDirName es una cadena vacía (''").
Se intentó mover un directorio a un volumen diferente.
O bien
destDirName ya existe.
O bien
No está autorizado para acceder a esta ruta de acceso.
O bien
El directorio que se mueve y el directorio de destino tienen el mismo nombre.
El autor de la llamada no tiene el permiso necesario.
No se encuentra el directorio de destino.
Ejemplos
En el ejemplo siguiente se muestra cómo mover un directorio.
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)
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("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 parámetro destDirName 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 comunes de E/S, consulte Tareas comunes de E/S.