DirectoryInfo.MoveTo(String) Metoda

Definicja

Przenosi wystąpienie DirectoryInfo i jego zawartość do nowej ścieżki.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)

Parametry

destDirName
String

Nazwa i ścieżka, do której ma zostać przeniesiony ten katalog.The name and path to which to move this directory. Lokalizacja docelowa nie może być innym woluminem dysku ani katalogiem o identycznej nazwie.The destination cannot be another disk volume or a directory with the identical name. Może to być katalog istniejący, do którego chcesz dodać ten katalog jako podkatalog.It can be an existing directory to which you want to add this directory as a subdirectory.

Wyjątki

Parametr destDirName ma wartość null.destDirName is null.

destDirName jest pustym ciągiem ("" ").destDirName is an empty string (''").

Podjęto próbę przeniesienia katalogu do innego woluminu.An attempt was made to move a directory to a different volume.

— lub —-or- destDirName już istnieje.destDirName already exists.

— lub —-or- Nie masz uprawnień dostępu do tej ścieżki.You are not authorized to access this path.

— lub —-or- Przeniesiony katalog i katalog docelowy mają tę samą nazwę.The directory being moved and the destination directory have the same name.

Element wywołujący nie ma wymaganego uprawnienia.The caller does not have the required permission.

Nie można znaleźć katalogu docelowego.The destination directory cannot be found.

Przykłady

Poniższy przykład demonstruje przeniesienie katalogu.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

Uwagi

Ta metoda zgłasza IOException, jeśli na przykład próbujesz przenieść c:\mydir do c:\public, a c:\public już istnieje.This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. Należy określić wartość "c:\\public\\mydir" jako parametr destDirName lub określić nową nazwę katalogu, na przykład "c:\\newdir".You must specify "c:\\public\\mydir" as the destDirName parameter, or specify a new directory name such as "c:\\newdir".

Ta metoda umożliwia przeniesienie katalogu do katalogu tylko do odczytu.This method permits moving a directory to a read-only directory. Nie dotyczy atrybutu odczytu/zapisu w żadnym katalogu.The read/write attribute of neither directory is affected.

Aby uzyskać listę typowych zadań we/wy, zobacz typowe zadania we/wy.For a list of common I/O tasks, see Common I/O Tasks.

Dotyczy

Zobacz też