DirectoryInfo.MoveTo(String) Método

Definição

Move uma instância DirectoryInfo e 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 os quais esse diretório será movido. 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 você deseja adicionar esse diretório como um subdiretório.

Exceções

destDirName é null.

destDirName é uma cadeia de caracteres vazia (''").

Tentativa de mover um diretório para um volume diferente.

- ou - destDirName já existe.

- ou - Você não está autorizado a acessar esse caminho.

- ou - O diretório que está sendo movido e o diretório de destino têm o mesmo nome.

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

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

Exemplos

O exemplo a seguir demonstra como mover um diretório.

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

Comentários

Esse método gera um IOException If, por exemplo, você tenta mover c:\mydir para c:\public e c:\public já existe. Você deve especificar "c: \ \Public \ \mydir" como o destDirName parâmetro ou especificar um novo nome de diretório, como "c: \\newdir".

Esse método permite mover um diretório para um diretório somente leitura. O atributo de leitura/gravação de nenhum diretório é afetado.

Para obter uma lista de tarefas comuns de e/s, consulte tarefas comuns de e/s.

Aplica-se a

Confira também