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

File.Move メソッド

指定したファイル新しい場所に移動しますオプション新しファイル名指定することもできます

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

Public Shared Sub Move (
 _
    sourceFileName As String, _
    destFileName As String _
)
Dim sourceFileName As String
Dim destFileName As String

File.Move(sourceFileName, destFileName)
public static void Move
 (
    string sourceFileName,
    string destFileName
)
public:
static void Move (
    String^ sourceFileName, 
    String^ destFileName
)
public static void Move
 (
    String sourceFileName, 
    String destFileName
)
public static function Move
 (
    sourceFileName : String, 
    destFileName : String
)

パラメータ

sourceFileName

移動するファイルの名前。

destFileName

ファイル新しパス

例外例外
例外種類条件

IOException

移動先のファイルは既に存在します

ArgumentNullException

sourceFileName または destFileNamenull 参照 (Visual Basic では Nothing) です。

ArgumentException

sourceFileName または destFileName が、長さが 0 の文字列であるか、空白しか含んでいないか、InvalidPathChars で定義されている無効な文字含んでます。

UnauthorizedAccessException

呼び出し元に必要なアクセス許可がありません。

FileNotFoundException

sourceFileName は見つかりませんでした

PathTooLongException

指定したパスファイル名、またはその両方システム定義の最大長を超えてます。たとえば、Windows ベースプラットフォーム場合パス長さ248 文字未満ファイル名長さ260 文字未満である必要があります

DirectoryNotFoundException

sourceFileName または destFileName指定したパス無効です (割り当てられていないドライブであるなど)。

NotSupportedException

sourceFileName または destFileName形式無効です。

解説解説

このメソッドは、複数ディスク ボリュームわたって動作し移動元と移動先が同じ場合は、例外スローしません。そのディレクトリに同じ名前のファイル移動してファイル置き換えようとすると、IOException発生しますMove メソッド使用して既存ファイル上書きすることはできません。

sourceFileName 引数および destFileName 引数は、相対パス情報または絶対パス情報指定することを許可されています。相対パス情報は、現在の作業ディレクトリに対して相対的に解釈されます。現在の作業ディレクトリ取得するには、GetCurrentDirectory のトピック参照してください

このメソッド使用例については、「使用例」を参照してくださいその他の一般的な I/O タスクまたは関連する I/O タスクの例を次の表に示します

使用例使用例

ファイル移動する例を次に示します

Imports System
Imports System.IO
Imports System.Text

Public Class Test
    Public Shared Sub Main()
        Dim path As String
 = "c:\temp\MyTest.txt"
        Dim path2 As String
 = "c:\temp2\MyTest.txt"

        Try
            If File.Exists(path) = False Then
                ' This statement ensures that the file is created,
                ' but the handle is not kept.
                Dim fs As FileStream = File.Create(path)
                fs.Close()
            End If

            ' Ensure that the target does not exist.
            If File.Exists(path2) Then
                File.Delete(path2)
            End If

            ' Move the file.
            File.Move(path, path2)
            Console.WriteLine("{0} moved to {1}",
 path, path2)

            ' See if the original file exists now.
            If File.Exists(path) Then
                Console.WriteLine("The original file still exists,
 which is unexpected.")
            Else
                Console.WriteLine("The original file no longer
 exists, which is expected.")
            End If
        Catch e As Exception
            Console.WriteLine("The process failed: {0}",
 e.ToString())
        End Try
    End Sub
End Class
using System;
using System.IO;

class Test 
{
    public static void Main()
 
    {
        string path = @"c:\temp\MyTest.txt";
        string path2 = @"c:\temp2\MyTest.txt";
        try 
        {
            if (!File.Exists(path)) 
            {
                // This statement ensures that the file is created,
                // but the handle is not kept.
                using (FileStream fs = File.Create(path)) {}
            }

            // Ensure that the target does not exist.
            if (File.Exists(path2))    
            File.Delete(path2);

            // Move the file.
            File.Move(path, path2);
            Console.WriteLine("{0} was moved to {1}.", path, path2);

            // See if the original exists now.
            if (File.Exists(path)) 
            {
                Console.WriteLine("The original file still exists, which is
 unexpected.");
            } 
            else 
            {
                Console.WriteLine("The original file no longer exists, which
 is expected.");
            }            

        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}
using namespace System;
using namespace System::IO;

int main()
{
   String^ path = "c:\\temp\\MyTest.txt";
   String^ path2 = "c:\\temp2\\MyTest.txt";
   try
   {
      if (  !File::Exists( path ) )
      {
         
         // This statement ensures that the file is created,
         // but the handle is not kept.
         FileStream^ fs = File::Create( path );
         if ( fs )
                  delete (IDisposable^)fs;
      }
      
      // Ensure that the target does not exist.
      if ( File::Exists( path2 ) )
            File::Delete( path2 );
      
      // Move the file.
      File::Move( path, path2 );
      Console::WriteLine( "{0} was moved to {1}.", path, path2 );
      
      // See if the original exists now.
      if ( File::Exists( path ) )
      {
         Console::WriteLine( "The original file still exists, which is unexpected."
 );
      }
      else
      {
         Console::WriteLine( "The original file no longer exists, which is expected."
 );
      }
   }
   catch ( Exception^ e ) 
   {
      Console::WriteLine( "The process failed: {0}", e );
   }
}
import System.*;
import System.IO.*;

class Test
{
    public static void main(String[]
 args)
    {
        String path = "c:\\temp\\MyTest.txt";
        String path2 = "c:\\temp2\\MyTest.txt";

        try {
            if (!(File.Exists(path))) {
                // This statement ensures that the file is created,
                // but the handle is not kept.
                FileStream fs = File.Create(path);

                try {
                }
                finally {
                    fs.Dispose();
                }                
            }

            // Ensure that the target does not exist.
            if (File.Exists(path2)) {
                File.Delete(path2);
            }

            // Move the file.
            File.Move(path, path2);
            Console.WriteLine("{0} was moved to {1}.", path, path2);

            // See if the original exists now.
            if (File.Exists(path)) {
                Console.WriteLine("The original file still exists, " 
                    + "which is unexpected.");
            }
            else {
                Console.WriteLine("The original file no longer exists, "
 
                    + "which is expected.");
            }
        }
        catch (System.Exception e) {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    } //main
} //Test
.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照


このページでは「.NET Framework クラス ライブラリ リファレンス」からFile.Move メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からFile.Move メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からFile.Move メソッド を検索

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

辞書ショートカット

すべての辞書の索引

「File.Move メソッド」の関連用語

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

   

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



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

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

©2025 GRAS Group, Inc.RSS