DirectoryInfo.MoveTo(String) Metodo

Definizione

Sposta un'istanza di DirectoryInfo e il relativo contenuto in un nuovo percorso.

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. La destinazione non può essere un altro volume del disco o una directory con lo stesso nome. Può essere una directory esistente alla quale si intende aggiungere questa directory come sottodirectory.

Eccezioni

destDirName è null.

Il parametrodestDirName è una stringa vuota.

È stato effettuato un tentativo di spostare una directory in un diverso volume.

-oppure-

destDirName esiste già.

-oppure-

Non si è autorizzati ad accedere a questo percorso.

-oppure-

La directory spostata e quella di destinazione hanno lo stesso nome.

Il chiamante non dispone dell'autorizzazione richiesta.

Non è possibile trovare la directory di destinazione.

Esempio

Nell'esempio seguente viene illustrato lo spostamento di una 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);
    }
}
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

Commenti

Questo metodo genera un'eccezione IOException se, ad esempio, si tenta di spostare c:\mydir in c:\public e c:\public esiste già. È necessario specificare "c:\\public\\mydir" come destDirName parametro oppure specificare un nuovo nome di directory, ad esempio "c:\\newdir".

Questo metodo consente di spostare una directory in una directory di sola lettura. L'attributo di lettura/scrittura di nessuna delle due directory è interessato.

Per un elenco delle attività di I/O comuni, vedere Attività di I/O comuni.

Si applica a

Vedi anche