次の方法で共有


File.Move メソッド

定義

オーバーロード

名前 説明
Move(String, String)

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

Move(String, String, Boolean)

指定したファイルを新しい場所に移動し、新しいファイル名を指定するオプションと、コピー先ファイルが既に存在する場合は置き換えるオプションを指定します。

Move(String, String)

ソース:
File.cs
ソース:
File.cs
ソース:
File.cs
ソース:
File.cs
ソース:
File.cs

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

public:
 static void Move(System::String ^ sourceFileName, System::String ^ destFileName);
public static void Move(string sourceFileName, string destFileName);
static member Move : string * string -> unit
Public Shared Sub Move (sourceFileName As String, destFileName As String)

パラメーター

sourceFileName
String

移動するファイルの名前。 相対パスまたは絶対パスを含めることができます。

destFileName
String

ファイルの新しいパスと名前。

例外

destFileName は既に存在します。

-又は-

ディスク ボリューム間でファイルをコピー中など、I/O エラーが発生しました。

sourceFileName が見つかりませんでした。

sourceFileName または destFileNamenull

.NET Framework および .NET Core バージョン 2.1 より前: sourceFileName または destFileName は長さ 0 の文字列、空白のみを含む、または無効な文字が含まれています。 GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。

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

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

sourceFileNameまたはdestFileNameで指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。

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

次の例では、ファイルを移動します。

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());
        }
    }
}
open System.IO

let path = @"c:\temp\MyTest.txt"
let path2 = @"c:\temp2\MyTest.txt"

if File.Exists path |> not then
    // This statement ensures that the file is created,
    // but the handle is not kept.
    use _ = File.Create path
    ()

// Ensure that the target does not exist.
if File.Exists path2 then
    File.Delete path2

// Move the file.
File.Move(path, path2)
printfn $"{path} was moved to {path2}."

// See if the original exists now.
if File.Exists path then
    printfn "The original file still exists, which is unexpected."
else
    printfn "The original file no longer exists, which is expected."
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

注釈

このメソッドはディスク ボリューム間で動作し、ソースと宛先が同じ場合は例外をスローしません。

同じ名前のファイルをそのディレクトリに移動してファイルを置き換えようとすると、 IOException がスローされることに注意してください。 この問題を回避するには:

  • .NET Core 3.0 以降のバージョンでは、パラメーター overwritetrue に設定Move(String, String, Boolean)呼び出すことができます。パラメーターが存在する場合は、ファイルが置き換えられます。

  • すべての .NET バージョンでは、 Copy(String, String, Boolean) を呼び出して上書きしてコピーし、 Delete を呼び出して余分なソース ファイルを削除できます。 この方法はアトミックではありません。 Copy 中にシステムまたはプログラムがクラッシュすると、部分的に書き込まれた宛先ファイルが残る可能性がありますが、(不完全な可能性がある) ファイルが常に宛先に存在することが保証されるためです。

  • すべての .NET バージョンでは、Moveを呼び出す前にDelete(String)を呼び出すことができます。これは、ファイルが存在する場合にのみ削除されます。

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

ディスク ボリューム間でのファイルの移動は、ファイルをコピーし、コピーが成功した場合にソースから削除することと同じです。

ファイルをディスク ボリューム間で移動しようとして、そのファイルが使用中の場合、ファイルはコピー先にコピーされますが、ソースから削除されません。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象

Move(String, String, Boolean)

ソース:
File.cs
ソース:
File.cs
ソース:
File.cs
ソース:
File.cs
ソース:
File.cs

指定したファイルを新しい場所に移動し、新しいファイル名を指定するオプションと、コピー先ファイルが既に存在する場合は置き換えるオプションを指定します。

public:
 static void Move(System::String ^ sourceFileName, System::String ^ destFileName, bool overwrite);
public static void Move(string sourceFileName, string destFileName, bool overwrite);
static member Move : string * string * bool -> unit
Public Shared Sub Move (sourceFileName As String, destFileName As String, overwrite As Boolean)

パラメーター

sourceFileName
String

移動するファイルの名前。 相対パスまたは絶対パスを含めることができます。

destFileName
String

ファイルの新しいパスと名前。

overwrite
Boolean

true コピー先ファイルが既に存在する場合は置き換えます。それ以外の場合 false

例外

destFileName は既に存在し、 overwritefalse

-又は-

ディスク ボリューム間でファイルをコピー中など、I/O エラーが発生しました。

sourceFileName が見つかりませんでした。

sourceFileName または destFileNamenull

.NET Framework および .NET Core バージョン 2.1 より前: sourceFileName または destFileName は長さ 0 の文字列、空白のみを含む、または無効な文字が含まれています。 GetInvalidPathChars() メソッドを使用して、無効な文字のクエリを実行できます。

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

-又は-

オペレーティング システムは、移行先ファイルへの排他的アクセスを取得できませんでした。

指定したパス、ファイル名、またはその両方が、システム定義の最大長を超えています。

sourceFileNameまたはdestFileNameで指定されたパスが無効です (たとえば、マップされていないドライブ上にあります)。

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

次の例では、ファイルを移動します。

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());
        }
    }
}
open System.IO

let path = @"c:\temp\MyTest.txt"
let path2 = @"c:\temp2\MyTest.txt"

if File.Exists path |> not then
    // This statement ensures that the file is created,
    // but the handle is not kept.
    use _ = File.Create path
    ()

// Ensure that the target does not exist.
if File.Exists path2 then
    File.Delete path2

// Move the file.
File.Move(path, path2)
printfn $"{path} was moved to {path2}."

// See if the original exists now.
if File.Exists path then
    printfn "The original file still exists, which is unexpected."
else
    printfn "The original file no longer exists, which is expected."
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

注釈

このメソッドはディスク ボリューム間で動作し、ソースと宛先が同じ場合は例外をスローしません。

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

ディスク ボリューム間でのファイルの移動は、ファイルをコピーし、コピーが成功した場合にソースから削除することと同じです。

ファイルをディスク ボリューム間で移動しようとして、そのファイルが使用中の場合、ファイルはコピー先にコピーされますが、ソースから削除されません。

一般的な I/O タスクの一覧については、「 一般的な I/O タスク」を参照してください。

こちらもご覧ください

適用対象