.NET Framework クラス ライブラリ
File..::.Move メソッド

更新 : 2007 年 11 月

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

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

Visual Basic (宣言)
Public Shared Sub Move ( _
    sourceFileName As String, _
    destFileName As String _
)
Visual Basic (使用法)
Dim sourceFileName As String
Dim destFileName As String

File.Move(sourceFileName, destFileName)
C#
public static void Move(
    string sourceFileName,
    string destFileName
)
Visual C++
public:
static void Move(
    String^ sourceFileName, 
    String^ destFileName
)
J#
public static void Move(
    String sourceFileName,
    String destFileName
)
JScript
public static function Move(
    sourceFileName : String, 
    destFileName : String
)

パラメータ

sourceFileName
型 : System..::.String
移動するファイルの名前。
destFileName
型 : System..::.String
ファイルの新しいパス。
例外

例外条件
IOException

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

ArgumentNullException

sourceFileName または destFileNamenullNothingnullptrnull 参照 (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 タスク」を参照してください。


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

Visual Basic
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
C#
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());
        }
    }
}
Visual C++
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 );
   }
}
J#
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
アクセス許可

プラットフォーム

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360

.NET Framework および .NET Compact Framework では、各プラットフォームのすべてのバージョンはサポートしていません。サポートされているバージョンについては、「.NET Framework システム要件」を参照してください。
バージョン情報

.NET Framework

サポート対象 : 3.5、3.0、2.0、1.1、1.0

.NET Compact Framework

サポート対象 : 3.5、2.0、1.0

XNA Framework

サポート対象 : 2.0、1.0
参照

参照

その他の技術情報

タグ :


Page view tracker