Freigeben über


DirectoryInfo.MoveTo-Methode

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

Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)

Syntax

'Declaration
Public Sub MoveTo ( _
    destDirName As String _
)
'Usage
Dim instance As DirectoryInfo
Dim destDirName As String

instance.MoveTo(destDirName)
public void MoveTo (
    string destDirName
)
public:
void MoveTo (
    String^ destDirName
)
public void MoveTo (
    String destDirName
)
public function MoveTo (
    destDirName : String
)

Parameter

  • destDirName
    Der Name und Pfad des Verzeichnisses, in das das Verzeichnis verschoben werden soll. Das Ziel darf keine andere Festplatte und kein Verzeichnis mit dem gleichen Namen sein. Es kann ein vorhandenes Verzeichnis sein, dem dieses Verzeichnis als Unterverzeichnis hinzugefügt werden soll.

Ausnahmen

Ausnahmetyp Bedingung

ArgumentNullException

destDirName ist NULL (Nothing in Visual Basic).

- oder -

Das verschobene Verzeichnis und das Zielverzeichnis haben denselben Namen.

ArgumentException

destDirName ist eine leere Zeichenfolge (''").

IOException

Es wurde versucht, ein Verzeichnis auf einen anderen Datenträger zu verschieben, oder destDirName ist bereits vorhanden.

SecurityException

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

Hinweise

Diese Methode löst eine IOException aus, wenn Sie z. B. versuchen, c:\mydir nach c:\public zu verschieben und c:\public bereits vorhanden ist. Sie müssen "c:\\public\\mydir" als den destDirName-Parameter angeben oder einen neuen Verzeichnisnamen angeben, z. B. "c:\\newdir".

Diese Methode erlaubt das Verschieben eines Verzeichnisses in ein schreibgeschütztes Verzeichnis. Das Lese-/Schreib-Attribut der beiden Verzeichnisse wird nicht geändert.

In der folgenden Tabelle sind Beispiele für andere typische oder verwandte E/A-Aufgaben aufgeführt.

Aufgabe

Beispiel in diesem Thema

Erstellen einer Textdatei.

Gewusst wie: Schreiben von Text in eine Datei

In eine Textdatei schreiben.

Gewusst wie: Schreiben von Text in eine Datei

Aus einer Textdatei lesen.

Gewusst wie: Lesen aus einer Textdatei

Löschen eines Verzeichnisses.

Directory.Delete

DirectoryInfo.Delete

Erstellen eines Verzeichnisses.

CreateDirectory

Directory

Erstellen eines Unterverzeichnisses.

CreateSubdirectory

Anzeigen der Dateien in einem Verzeichnis.

Name

Anzeigen der Unterverzeichnisse in einem Verzeichnis.

GetDirectories

GetDirectories

Anzeigen aller Dateien und Unterverzeichnisse in einem Verzeichnis.

GetFileSystemInfos

Ermitteln der Größe eines Verzeichnisses.

Directory

Bestimmen, ob eine Datei vorhanden ist.

Exists

Bestimmen, ob ein Verzeichnis vorhanden ist.

Exists

Beispiel

Das folgende Beispiel veranschaulicht das Verschieben eines Verzeichnisses.

Imports System
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 'Main
End Class 'MoveToTest
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);
    }
}
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);
}
import System.*;
import System.IO.*;

public class MoveToTest
{
    public static void main(String[] args)
    {
        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.get_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 (System.Exception exp) {
            // 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);
    } //main
} //MoveToTest 
import System;
import System.IO;

public class MoveToTest {
    public static function Main() {

        // Make a reference to a directory.
        var di : DirectoryInfo = 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.
        var dis : DirectoryInfo = di.CreateSubdirectory("SubDir");

        // Move the main directory. Note that its contents move also.
        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, 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);
    }
}
MoveToTest.Main();

.NET Framework-Sicherheit

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

DirectoryInfo-Klasse
DirectoryInfo-Member
System.IO-Namespace

Weitere Ressourcen

Datei- und Stream-E/A
Gewusst wie: Lesen aus einer Textdatei
Gewusst wie: Schreiben von Text in eine Datei