DirectoryInfo.MoveTo(String) 方法

定义

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

要将此目录移动到的目标位置的名称和路径。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.

异常

destDirNamenulldestDirName is null.

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

注解

例如,如果尝试将 c:\mydir 移动到 c:\public,并且 c:\public 已存在,则此方法会引发 IOExceptionThis method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. 必须将 "c:\\ 公用\\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.

适用于

另请参阅