DirectoryInfo.MoveTo(String) Método

Definição

Move uma DirectoryInfo instância e o seu conteúdo para um novo caminho.

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

O nome e o caminho para onde mover este diretório. O destino não pode ser outro volume de disco ou um diretório com o mesmo nome. Pode ser um diretório existente ao qual pretende adicionar esse diretório como subdiretório.

Exceções

destDirName é null.

destDirName é uma corda vazia (''").

Foi feita uma tentativa de mover um diretório para um volume diferente.

-ou-

destDirName já existe.

-ou-

Não está autorizado a aceder a este caminho.

-ou-

O diretório a ser movido e o diretório de destino têm o mesmo nome.

O interlocutor não tem a permissão necessária.

O diretório de destino não pode ser encontrado.

Exemplos

O exemplo seguinte demonstra como mover um diretório.

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

Observações

Este método lança um IOException se, por exemplo, tentares mover c:\mydir para c:\public, e c:\public já existe. Deve especificar "c:\\public\\mydir" como destDirName parâmetro, ou especificar um novo nome de diretório como "c:\\newdir".

Este método permite mover um diretório para um diretório de apenas leitura. O atributo de leitura/escrita de nenhum dos diretórios é afetado.

Para uma lista de tarefas comuns de E/S, consulte Tarefas Comuns de E/S.

Aplica-se a

Ver também