DirectoryInfo.MoveTo メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > DirectoryInfo.MoveTo メソッドの意味・解説 

DirectoryInfo.MoveTo メソッド

DirectoryInfo のインスタンスその内容新しパス移動します

名前空間: System.IO
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Sub MoveTo ( _
    destDirName As String _
)
Dim instance As DirectoryInfo
Dim destDirName As String

instance.MoveTo(destDirName)
public void MoveTo (
    string destDirName
)
public:
void MoveTo (
    String^ destDirName
)
public void MoveTo (
    String destDirName
)
public function MoveTo (
    destDirName : String
)

パラメータ

destDirName

このディレクトリ移動先の名前とパス別のディスク ボリュームまたは同じ名前のディレクトリ移動先として指定できません。このディレクトリサブディレクトリとして追加する場合は、既存ディレクトリ指定できます

例外例外
例外種類条件

ArgumentNullException

destDirNamenull 参照 (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 タスクの例を次の表に示します

使用例使用例

ディレクトリ移動する例を次に示します

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

import System.*;
import System.IO.*;

public class MoveToTest
{
    public static void main(String[]
 args)
    {
        // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("TempDir");

        // Create the directory only if it does not already exist.
        if (di.get_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 (System.Exception exp) {
            // 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);
    } //main
} //MoveToTest 
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();
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照



英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「DirectoryInfo.MoveTo メソッド」の関連用語

DirectoryInfo.MoveTo メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



DirectoryInfo.MoveTo メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS