Personas que lo han encontrado útil: 0 de 1 - Valorar este tema

DirectoryInfo.MoveTo (Método)

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

Espacio de nombres: System.IO
Ensamblado: mscorlib (en mscorlib.dll)

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

Parámetros

destDirName

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.

Tipo de excepción Condición

ArgumentNullException

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.

ArgumentException

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

IOException

Se ha intentado mover un directorio a un volumen diferente, o destDirName ya existe.

SecurityException

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

Cómo: Escribir texto en un archivo

Escribir en un archivo de texto

Cómo: Escribir texto en un archivo

Leer de un archivo de texto

Cómo: Leer texto de un archivo

Eliminar un directorio

Directory.Delete

DirectoryInfo.Delete

Crear un directorio

CreateDirectory

Directory

Crear un subdirectorio

CreateSubdirectory

Ver los archivos de un directorio

Name

Ver los subdirectorios de un directorio

GetDirectories

GetDirectories

Ver todos los archivos y todos los subdirectorios de un directorio

GetFileSystemInfos

Obtener el tamaño de un directorio

Directory

Determinar si un archivo existe

Exists

Determinar si un directorio existe

Exists

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();

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.

.NET Framework

Compatible con: 2.0, 1.1, 1.0

.NET Compact Framework

Compatible con: 2.0, 1.0
¿Le ha resultado útil?
(Caracteres restantes: 1500)
Contenido de la comunidad Agregar