DirectoryInfo.MoveTo(String) Méthode

Définition

Déplace une instance de DirectoryInfo et son contenu dans un nouveau chemin d’accès.Moves a DirectoryInfo instance and its contents to a new path.

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.The name and path to which to move this directory. La destination ne peut pas être un autre volume de disque ni un répertoire du même nom.The destination cannot be another disk volume or a directory with the identical name. Il peut s'agir d'un répertoire existant dans lequel vous souhaitez ajouter ce répertoire en tant que sous-répertoire.It can be an existing directory to which you want to add this directory as a subdirectory.

Exceptions

destDirName a la valeur null.destDirName is null.

destDirName est une chaîne vide ("").destDirName is an empty string (''").

Une tentative a été effectuée pour déplacer un répertoire vers un autre volume.An attempt was made to move a directory to a different volume.

- ou --or- destDirName existe déjà.destDirName already exists.

- ou --or- Vous n'êtes pas autorisé à accéder à ce chemin d'accès.You are not authorized to access this path.

- ou --or- Le répertoire déplacé et le répertoire de destination ont le même nom.The directory being moved and the destination directory have the same name.

L'appelant n'a pas l'autorisation requise.The caller does not have the required permission.

Le répertoire de destination est introuvable.The destination directory cannot be found.

Exemples

L’exemple suivant illustre le déplacement d’un répertoire.The following example demonstrates moving a directory.

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à.This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. Vous devez spécifier « c : \ \Public \ \MyDir » comme destDirName paramètre, ou spécifier un nouveau nom de répertoire, tel que « c : \ \Newdir ».You must specify "c:\\public\\mydir" as the destDirName parameter, or specify a new directory name such as "c:\\newdir".

Cette méthode permet de déplacer un répertoire vers un répertoire en lecture seule.This method permits moving a directory to a read-only directory. L’attribut lecture/écriture d’aucun répertoire n’est affecté.The read/write attribute of neither directory is affected.

Pour obtenir la liste des tâches d’e/s courantes, consultez tâches d’e/s courantes.For a list of common I/O tasks, see Common I/O Tasks.

S’applique à

Voir aussi