DirectoryInfo.MoveTo(String) Metod

Definition

Flyttar en DirectoryInfo instans och dess innehåll till en ny sökväg.

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

Parametrar

destDirName
String

Namnet och sökvägen som du vill flytta katalogen till. Målet får inte vara en annan diskvolym eller en katalog med samma namn. Det kan vara en befintlig katalog som du vill lägga till den här katalogen i som en underkatalog.

Undantag

destDirName är null.

destDirName är en tom sträng (''").

Ett försök gjordes att flytta en katalog till en annan volym.

-eller-

destDirName redan finns.

-eller-

Du har inte behörighet att komma åt den här sökvägen.

-eller-

Katalogen som flyttas och målkatalogen har samma namn.

Anroparen har inte den behörighet som krävs.

Det går inte att hitta målkatalogen.

Exempel

I följande exempel visas hur du flyttar en katalog.

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

Kommentarer

Den här metoden genererar ett IOException om du till exempel försöker flytta c:\mydir till c:\public och c:\public redan finns. Du måste ange "c:\\public\\mydir" som destDirName parameter eller ange ett nytt katalognamn, till exempel "c:\\newdir".

Med den här metoden kan du flytta en katalog till en skrivskyddad katalog. Läs-/skrivattributet för ingen av katalogerna påverkas.

En lista över vanliga I/O-uppgifter finns i Vanliga I/O-uppgifter.

Gäller för

Se även