DirectoryInfo.MoveTo(String) Methode

Definition

Verschiebt eine DirectoryInfo Instanz und deren Inhalt in einen neuen Pfad.

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

Parameter

destDirName
String

Der Name und der Pfad, in den dieses Verzeichnis verschoben werden soll. Das Ziel kann kein anderes Datenträgervolume oder ein Verzeichnis mit demselben Namen sein. Es kann sich um ein vorhandenes Verzeichnis sein, dem Sie dieses Verzeichnis als Unterverzeichnis hinzufügen möchten.

Ausnahmen

destDirName ist null.

destDirName ist eine leere Zeichenfolge ("'").

Es wurde versucht, ein Verzeichnis auf ein anderes Volume zu verschieben.

-oder-

destDirName ist bereits vorhanden.

-oder-

Sie sind nicht berechtigt, auf diesen Pfad zuzugreifen.

-oder-

Das verschobene Verzeichnis und das Zielverzeichnis haben denselben Namen.

Der Aufrufer verfügt nicht über die erforderliche Berechtigung.

Das Zielverzeichnis wurde nicht gefunden.

Beispiele

Im folgenden Beispiel wird das Verschieben eines Verzeichnisses veranschaulicht.

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

Hinweise

Mit dieser Methode IOException wird beispielsweise versucht, "c:\mydir" in "c:\public" zu verschieben, und "c:\public" ist bereits vorhanden. Sie müssen "c:\\public\\mydir" als destDirName Parameter angeben oder einen neuen Verzeichnisnamen wie "c:\\newdir" angeben.

Diese Methode ermöglicht das Verschieben eines Verzeichnisses in ein schreibgeschütztes Verzeichnis. Das Lese-/Schreib-Attribut von keinem Verzeichnis ist betroffen.

Eine Liste allgemeiner E/A-Aufgaben finden Sie unter "Allgemeine E/A-Aufgaben".

Gilt für:

Weitere Informationen