DirectoryInfo.MoveTo (Método)
Ensamblado: mscorlib (en mscorlib.dll)
| Tipo de excepción | Condición |
|---|---|
| destDirName es referencia de objeto null (Nothing en Visual Basic). O bien El directorio que se está moviendo y el directorio de destino tienen el mismo nombre. |
|
| destDirName es una cadena vacía (""). |
|
| Se ha intentado mover un directorio a un volumen diferente, o destDirName ya existe. |
|
| El llamador no dispone del permiso requerido. |
Este método produce una excepción IOException si, por ejemplo, se intenta mover c:\mydir a c:\public, y c:\public ya existe. Debe especificar "c:\\public\\MiDir" como parámetro destDirName, o indicar un nuevo nombre de directorio, como "c:\\NuevoDir".
Este método permite mover un directorio a otro de sólo lectura. El atributo de lectura y escritura de los directorios no se ve afectado.
En la siguiente tabla se muestran ejemplos de otras tareas de E/S típicas o relacionadas.
| Para realizar esta operación... | Vea el ejemplo de este tema... |
|---|---|
| Crear un archivo de texto | |
| Escribir en un archivo de texto | |
| Leer de un archivo de texto | |
| Eliminar un directorio | |
| Crear un directorio | |
| Crear un subdirectorio | |
| Ver los archivos de un directorio | |
| Ver los subdirectorios de un directorio | |
| Ver todos los archivos y todos los subdirectorios de un directorio | |
| Obtener el tamaño de un directorio | |
| Determinar si un archivo existe | |
| Determinar si un directorio existe |
En el siguiente ejemplo 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 == 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 para leer y escribir en archivos y directorios, y para tener acceso al directorio de destino. Enumeraciones asociadas: FileIOPermissionAccess.Read, FileIOPermissionAccess.Write.
Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium, Windows Mobile para Pocket PC, Windows Mobile para Smartphone, Windows Server 2003, Windows XP Media Center, Windows XP Professional x64, Windows XP SP2, Windows XP Starter Edition
.NET Framework no admite todas las versiones de cada plataforma. Para obtener una lista de las versiones admitidas, vea Requisitos del sistema.