DirectoryInfo.MoveTo(String) メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
DirectoryInfo インスタンスとその内容を新しいパスに移動します。
public:
void MoveTo(System::String ^ destDirName);
public void MoveTo(string destDirName);
member this.MoveTo : string -> unit
Public Sub MoveTo (destDirName As String)
パラメーター
- destDirName
- String
このディレクトリを移動する名前とパス。 宛先を別のディスク ボリュームまたは同じ名前のディレクトリにすることはできません。 このディレクトリをサブディレクトリとして追加する既存のディレクトリを指定できます。
例外
destDirName は nullです。
destDirName は空の文字列 (''") です。
ディレクトリを別のボリュームに移動しようとしました。
-又は-
destDirName は既に存在します。
-又は-
このパスにアクセスする権限がありません。
-又は-
移動するディレクトリと移動先ディレクトリの名前は同じです。
呼び出し元に必要なアクセス許可がありません。
宛先ディレクトリが見つかりません。
例
次の例では、ディレクトリの移動を示します。
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
注釈
たとえば、c:\mydir を c:\public に移動しようとして、c:\public が既に存在する場合、このメソッドは IOException をスローします。
destDirName パラメーターとして "c:\\public\\mydir" を指定するか、"c:\\newdir" などの新しいディレクトリ名を指定する必要があります。
このメソッドを使用すると、ディレクトリを読み取り専用ディレクトリに移動できます。 どちらのディレクトリの読み取り/書き込み属性も影響を受けます。
一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。