语言

DirectoryInfo.MoveTo(String) 方法

定义

将 DirectoryInfo 实例及其内容移动到新路径。

public:
 void MoveTo(System::String ^ destDirName);
public void MoveTo(string destDirName);
member this.MoveTo : string -> unit
Public Sub MoveTo (destDirName As String)

参数

destDirName
String

要移动此目录的名称和路径。 目标不能是另一个磁盘卷或具有相同名称的目录。 它可以是要将此目录添加为子目录的现有目录。

例外

destDirName 是 null。

destDirName 是空字符串('')。

尝试将目录移动到其他卷。

-或-

destDirName 已存在。

-或-

无权访问此路径。

-或-

要移动的目录和目标目录具有相同的名称。

调用方没有所需的权限。

找不到目标目录。

示例

以下示例演示如何移动目录。

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)
            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("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

注解

例如,此方法会引发一个 IOException if,例如,尝试将 c:\mydir 移动到 c:\public,并且 c:\public 已存在。 必须将“c:\\public\\mydir”指定为 destDirName 参数,或指定新的目录名称,例如“c:\\newdir”。

此方法允许将目录移动到只读目录。 两个目录的读/写属性均不受影响。

有关常见 I/O 任务的列表,请参阅 常见 I/O 任务。

适用于

另请参阅