Share via


File.Move メソッド

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

Public Shared Sub Move( _
   ByVal sourceFileName As String, _   ByVal destFileName As String _)
[C#]
public static void Move(stringsourceFileName,stringdestFileName);
[C++]
public: static void Move(String* sourceFileName,String* destFileName);
[JScript]
public static function Move(
   sourceFileName : String,destFileName : String);

パラメータ

  • sourceFileName
    移動するファイルの名前。
  • destFileName
    ファイルの新しいパス。

例外

例外の種類 条件
IOException 移動先のファイルは既に存在します。
ArgumentNullException sourceFileName または destFileName が null 参照 (Visual Basic では Nothing) です。
ArgumentException sourceFileName または destFileName が、長さが 0 の文字列であるか、空白しか含んでいないか、 InvalidPathChars で定義されている無効な文字を含んでいます。
UnauthorizedAccessException 呼び出し元に、必要なアクセス許可がありません。
FileNotFoundException sourceFileName は見つかりませんでした。
PathTooLongException 指定したパス、ファイル名、またはその両方がシステム定義の最大長を超えています。たとえば、Windows ベースのプラットフォームの場合、パスの長さは 248 文字未満、ファイル名の長さは 260 文字未満である必要があります。
DirectoryNotFoundException 割り当てられていないドライブであるなど、 sourceFileName または destFileName で指定されたパスが無効です。
NotSupportedException sourceFileName または destFileName の形式が無効です。

解説

このメソッドは、複数のディスク ボリュームにわたって動作し、移動元と移動先が同じ場合は、例外をスローしません。

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

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

実行するタスク 参考例があるトピック
テキスト ファイルに書き込む。 ファイルへのテキストの書き込み
テキスト ファイルから読み取る。 ファイルからのテキストの読み取り
テキストをファイルに追加する。 ログ ファイルのオープンと追加

File.AppendText

FileInfo.AppendText

ファイルをコピーする。 File.Copy

FileInfo.CopyTo

ディレクトリの名前を変更、またはディレクトリを移動する。 Directory.Move

DirectoryInfo.MoveTo

使用例

[Visual Basic, C#, C++] ファイルを移動する例を次に示します。

 
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 = path + "temp"

        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 = path + "temp";
        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());
        }
    }
}

[C++] 
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;

int main() {
    String* path = S"c:\\temp\\MyTest.txt";
    String* path2 = String::Concat(path, S"temp");
    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) __try_cast<IDisposable*>(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(S"{0} was moved to {1}.", path, path2);

        // See if the original exists now.
        if (File::Exists(path)) {
            Console::WriteLine(S"The original file still exists, which is unexpected.");
        } else {
            Console::WriteLine(S"The original file no longer exists, which is expected.");
        }            
    } catch (Exception* e) {
        Console::WriteLine(S"The process failed: {0}", e);
    }
}

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: 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, Common Language Infrastructure (CLI) Standard

.NET Framework セキュリティ:

参照

File クラス | File メンバ | System.IO 名前空間 | 入出力操作 | ファイルからのテキストの読み取り | ファイルへのテキストの書き込み