DirectoryInfo.MoveTo(String) Méthode

Définition

Déplace une instance de DirectoryInfo et son contenu dans un nouveau chemin d’accès.

public:
 void MoveTo(System::String ^ destDirName);
public void MoveTo (string destDirName);
member this.MoveTo : string -> unit
Public Sub MoveTo (destDirName As String)

Paramètres

destDirName
String

Nom et chemin d'accès selon lesquels déplacer ce répertoire. La destination ne peut pas être un autre volume de disque ni un répertoire du même nom. Il peut s'agir d'un répertoire existant dans lequel vous souhaitez ajouter ce répertoire en tant que sous-répertoire.

Exceptions

destDirName a la valeur null.

destDirName est une chaîne vide ("").

Une tentative a été effectuée pour déplacer un répertoire vers un autre volume.

  • ou - destDirName existe déjà.

  • ou - Vous n'êtes pas autorisé à accéder à ce chemin d'accès.

  • ou - Le répertoire déplacé et le répertoire de destination ont le même nom.

L'appelant n'a pas l'autorisation requise.

Le répertoire de destination est introuvable.

Exemples

L’exemple suivant illustre le déplacement d’un répertoire.

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

Remarques

Cette méthode lève une IOException si, par exemple, vous essayez de déplacer c:\MyDir vers c:\Public, et c:\Public existe déjà. Vous devez spécifier « c : \ \Public \ \MyDir » comme destDirName paramètre, ou spécifier un nouveau nom de répertoire, tel que « c : \\newdir ».

Cette méthode permet de déplacer un répertoire vers un répertoire en lecture seule. L’attribut lecture/écriture d’aucun répertoire n’est affecté.

Pour obtenir la liste des tâches d’e/s courantes, consultez tâches d’e/s courantes.

S’applique à

Voir aussi