DirectoryInfo.MoveTo(String) Metodo

Definizione

Sposta un'istanza di DirectoryInfo e il relativo contenuto in un nuovo percorso.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)

Parametri

destDirName
String

Nome e percorso nel quale spostare questa directory.The name and path to which to move this directory. La destinazione non può essere un altro volume del disco o una directory con lo stesso nome.The destination cannot be another disk volume or a directory with the identical name. Può essere una directory esistente alla quale si intende aggiungere questa directory come sottodirectory.It can be an existing directory to which you want to add this directory as a subdirectory.

Eccezioni

destDirName è null.destDirName is null.

Il parametrodestDirName è una stringa vuota.destDirName is an empty string (''").

È stato effettuato un tentativo di spostare una directory in un diverso volume.An attempt was made to move a directory to a different volume.

-oppure--or- destDirName esiste già.destDirName already exists.

-oppure--or- Non si è autorizzati ad accedere a questo percorso.You are not authorized to access this path.

-oppure--or- La directory spostata e quella di destinazione hanno lo stesso nome.The directory being moved and the destination directory have the same name.

Il chiamante non dispone dell'autorizzazione richiesta.The caller does not have the required permission.

Non è possibile trovare la directory di destinazione.The destination directory cannot be found.

Esempi

Nell'esempio seguente viene illustrato lo stato di trasferimento di una directory.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

Commenti

Questo metodo genera un' IOException eccezione se, ad esempio, si tenta di spostare c:\mydir in c:\public e c:\public esiste già.This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. È necessario specificare "c:\\Cartelle pubbliche\\MyDir" come destDirName parametro oppure specificare un nuovo nome di directory, ad esempio "c:\\NEWDIR".You must specify "c:\\public\\mydir" as the destDirName parameter, or specify a new directory name such as "c:\\newdir".

Questo metodo consente lo trasferimento di una directory in una directory di sola lettura.This method permits moving a directory to a read-only directory. L'attributo di lettura/scrittura di nessuna directory è interessato.The read/write attribute of neither directory is affected.

Per un elenco delle attività di I/O comuni, vedere attività di i/o comuni.For a list of common I/O tasks, see Common I/O Tasks.

Sicurezza

FileIOPermission
per la lettura e la scrittura di file e directory e per l'accesso alla directory di destinazione.for reading and writing files and directories and for access to the destination directory. Azione di sicurezza Demand:.Security action: Demand. Enumerazioni associate: Read,WriteAssociated enumerations: Read, Write

Si applica a

Vedi anche