DirectoryInfo.MoveTo(String) Methode

Definitie

Hiermee verplaatst u een DirectoryInfo exemplaar en de inhoud ervan naar een nieuw pad.

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

Parameters

destDirName
String

De naam en het pad waarnaar u deze map wilt verplaatsen. Het doel mag geen ander schijfvolume of een map met dezelfde naam zijn. Het kan een bestaande map zijn waaraan u deze map wilt toevoegen als submap.

Uitzonderingen

destDirName is null.

destDirName is een lege tekenreeks ('').'

Er is een poging gedaan om een map naar een ander volume te verplaatsen.

– of –

destDirName bestaat al.

– of –

U bent niet gemachtigd om toegang te krijgen tot dit pad.

– of –

De map die wordt verplaatst en de doelmap heeft dezelfde naam.

De beller heeft niet de vereiste machtiging.

Kan de doelmap niet vinden.

Voorbeelden

In het volgende voorbeeld ziet u hoe u een map verplaatst.

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

Opmerkingen

Met deze methode genereert u bijvoorbeeld een IOException als u c:\mydir naar c:\public wilt verplaatsen en c:\public al bestaat. U moet 'c:\\public\\mydir' opgeven als de destDirName parameter of een nieuwe mapnaam opgeven, zoals 'c:\\newdir'.

Met deze methode kunt u een map verplaatsen naar een alleen-lezen map. Het kenmerk lezen/schrijven van geen van beide directory's wordt beïnvloed.

Zie Algemene I/O-taken voor een lijst met algemene I/O-taken.

Van toepassing op

Zie ook