DirectoryInfo.MoveTo(String) DirectoryInfo.MoveTo(String) DirectoryInfo.MoveTo(String) DirectoryInfo.MoveTo(String) Method

定義

DirectoryInfo のインスタンスとその内容を新しいパスに移動します。Moves a DirectoryInfo instance and its contents to a new path.

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

パラメーター

destDirName
String String String String

このディレクトリの移動先の名前とパス。The name and path to which to move this directory. 別のディスク ボリュームまたは同じ名前のディレクトリは移動先として指定できません。The destination cannot be another disk volume or a directory with the identical name. このディレクトリをサブディレクトリとして追加する場合は、既存のディレクトリを指定できます。It can be an existing directory to which you want to add this directory as a subdirectory.

例外

destDirName が空の文字列 (''") です。destDirName is an empty string (''").

ディレクトリを別のボリュームに移動しようとしました。An attempt was made to move a directory to a different volume.

- または --or- destDirName が既に存在します。destDirName already exists.

- または --or- このパスへのアクセスが承認されていません。You are not authorized to access this path.

- または --or- 移動しようとしているディレクトリと移動先のディレクトリが同じ名前です。The directory being moved and the destination directory have the same name.

呼び出し元に、必要なアクセス許可がありません。The caller does not have the required permission.

移動先のディレクトリが見つかりません。The destination directory cannot be found.

次の例では、ディレクトリの移動を示します。The following example demonstrates moving a directory.

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);
}

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);
    }
}
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

注釈

たとえば、c:\mydir をIOException c:\ に移動しようとしていて、c:\ パブリックが既に存在する場合、このメソッドはをスローします。This method throws an IOException if, for example, you try to move c:\mydir to c:\public, and c:\public already exists. パラメーターと\して\\"c: \ public \mydir" を指定するか、"c: \ newdir" のような新しいディレクトリ名を指定する必要があります。 destDirNameYou must specify "c:\\public\\mydir" as the destDirName parameter, or specify a new directory name such as "c:\\newdir".

この方法を使うと、ディレクトリを読み取り専用ディレクトリに移動できます。This method permits moving a directory to a read-only directory. どちらのディレクトリの読み取り/書き込み属性も影響を受けません。The read/write attribute of neither directory is affected.

共通 I/O タスクの一覧は、 共通 I/O タスク を参照してください。For a list of common I/O tasks, see Common I/O Tasks.

セキュリティ

FileIOPermission
ファイルとディレクトリの読み取りと書き込み、および宛先ディレクトリへのアクセスに使用します。for reading and writing files and directories and for access to the destination directory. セキュリティアクション: DemandSecurity action: Demand. 関連付けられReadた列挙型:、WriteAssociated enumerations: Read, Write

適用対象

こちらもご覧ください