DirectoryInfo.MoveTo メソッド
DirectoryInfo のインスタンスとその内容を新しいパスに移動します。
Public Sub MoveTo( _
ByVal destDirName As String _)
[C#]
public void MoveTo(stringdestDirName);
[C++]
public: void MoveTo(String* destDirName);
[JScript]
public function MoveTo(
destDirName : String);
パラメータ
- destDirName
このディレクトリの移動先の名前とパス。別のディスク ボリュームまたは同じ名前のディレクトリは移動先として指定できません。このディレクトリをサブディレクトリとして追加する場合は、既存のディレクトリを指定できます。
例外
例外の種類 | 条件 |
---|---|
ArgumentNullException | destDirName が null 参照 (Visual Basic では Nothing) です。
または 移動しようとしているディレクトリと移動先のディレクトリが同じ名前です。 |
ArgumentException | destDirName が空の文字列 (''") です。 |
IOException | 異なるボリュームにディレクトリを移動しようとしました。または、 destDirName が既に存在しています。 |
SecurityException | 呼び出し元に、必要なアクセス許可がありません。 |
解説
たとえば、c:\mydir を c:\public に移動しようとしたときに c:\public が既に存在している場合、このメソッドは IOException をスローします。このような場合は、 destDirName パラメータとして "c:\\public\\mydir"を指定するか、"c:\\newdir" などの新しいディレクトリ名を指定する必要があります。
このメソッドでは、読み取り専用ディレクトリにディレクトリを移動できます。このとき、いずれのディレクトリの読み書き属性も影響されません。
このメソッドの使用例については、以下の「使用例」を参照してください。その他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します。
実行するタスク | 参考例があるトピック |
---|---|
テキスト ファイルを作成する。 | ファイルへのテキストの書き込み |
テキスト ファイルに書き込む。 | ファイルへのテキストの書き込み |
テキスト ファイルから読み取る。 | ファイルからのテキストの読み取り |
ディレクトリをコピーする。 | Directory |
ディレクトリを削除する。 | Directory.Delete |
ディレクトリを作成する。 | CreateDirectory |
サブディレクトリを作成する。 | CreateSubdirectory |
ディレクトリ内のファイルを参照する。 | Name |
ディレクトリ内のサブディレクトリを参照する。 | GetDirectories |
ディレクトリ内のすべてのサブディレクトリにあるすべてのファイルを参照する。 | GetFileSystemInfos |
ディレクトリのサイズを取得する。 | Directory |
ファイルが存在するかどうかを判別する。 | Exists |
ディレクトリが存在するかどうかを判別する。 | Exists |
使用例
ディレクトリを移動する例を次に示します。
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
[C#]
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);
}
}
[C++]
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
int main() {
// Make a reference to a directory.
DirectoryInfo* di = new DirectoryInfo(S"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(S"SubDir");
// Move the main directory. Note that the contents move with the directory.
if (!Directory::Exists(S"NewTempDir"))
di->MoveTo(S"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);
}
[JScript]
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();
必要条件
プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET
.NET Framework セキュリティ:
- FileIOPermissionAccess Write フラグ (移動先ディレクトリに書き込むために必要なアクセス許可)
参照
DirectoryInfo クラス | DirectoryInfo メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み