DirectoryInfo.MoveTo(String) DirectoryInfo.MoveTo(String) DirectoryInfo.MoveTo(String) DirectoryInfo.MoveTo(String) Method

定義

移動 DirectoryInfo 執行個體和其內容至新的路徑。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)

參數

destDirName
String String String String

要將這個目錄移至的目的名稱和路徑。The name and path to which to move this directory. 目的端不可以是另一個磁碟區,或者是名稱完全相同的目錄。The destination cannot be another disk volume or a directory with the identical name. 它可以是您要將這個目錄加入做為子目錄的現有目錄。It can be an existing directory to which you want to add this directory as a subdirectory.

例外狀況

destDirName 為空字串 ("")。destDirName is an empty string (''").

嘗試將目錄移至不同的磁碟區。An attempt was made to move a directory to a different volume.

-或--or- destDirName 已經存在。destDirName already exists.

-或--or- 您未取得存取此路徑的授權。You are not authorized to access this path.

-或--or- 正在移動的目錄與目的目錄具有相同名稱。The directory being moved and the destination directory have the same name.

呼叫端沒有必要的權限。The caller does not have the required permission.

找不到目的目錄。The destination directory cannot be found.

範例

下列範例示範如何移動目錄。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 'MoveToTest

備註

例如, IOException如果您嘗試將 c:\mydir 移至 c:\public,而且 c:\public 已經存在,這個方法就會擲回。This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. 您必須指定 "c:\\public\\mydir" 做為destDirName參數,或指定新的目錄名稱,例如 "c:\\newdir"。You must specify "c:\\public\\mydir" as the destDirName parameter, or specify a new directory name such as "c:\\newdir".

這個方法允許將目錄移到唯讀目錄。This method permits moving a directory to a read-only directory. 這兩個目錄的讀取/寫入屬性都會受到影響。The read/write attribute of neither directory is affected.

如需一般 i/o 工作的清單, 請參閱一般 i/o工作。For a list of common I/O tasks, see Common I/O Tasks.

安全性

FileIOPermission
用於讀取和寫入檔案和目錄,以及存取目的地目錄。for reading and writing files and directories and for access to the destination directory. 安全性動作: DemandSecurity action: Demand. 相關聯的Read列舉:、WriteAssociated enumerations: Read, Write

適用於

另請參閱