共用方式為


File.Move 方法

定義

多載

名稱 Description
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 未被找到。

sourceFileNamedestFileNamenull

.NET Framework 與 .NET Core 版本早於 2.1: sourceFileNamedestFileName 為零長度字串、僅含空白或無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。

來電者沒有所需的權限。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

destFileName 中指定的sourceFileName路徑無效(例如,它位於未映射的磁碟機上)。

sourceFileNamedestFileName 格式無效。

範例

以下範例移動一個檔案。

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

備註

此方法跨磁碟磁碟區運作,且若來源與目的地相同,則不會拋出例外。

請注意,如果你嘗試將同名檔案移到該目錄來替換檔案,會拋出 an IOException 。 為了避免這個問題:

  • 在 .NET Core 3.0 及更新版本中,你可以呼叫 Move(String, String, Boolean) 設定 overwrite 參數為 true,如果有該檔案,則會替換。

  • 在所有 .NET 版本中,你可以呼叫 Copy(String, String, Boolean) 複製並覆蓋,然後呼叫 Delete 移除多餘的原始碼檔案。 此策略並非原子性,因為系統或程式在 中 Copy 當機可能會留下部分寫入的目標檔案,但能確保(可能不完整的)檔案始終存在於目的地。

  • 在所有 .NET 版本中,你可以先呼叫 Delete(String) 再呼叫 Move,只有當檔案存在時才會刪除。

sourceFileNamedestFileName參數可包含相對或絕對路徑資訊。 相對路徑資訊會被解讀為相對於目前工作目錄的相對於。 欲取得目前的工作目錄,請參見 GetCurrentDirectory

跨磁碟區移動檔案等同於複製檔案後,若複製成功,則從來源刪除。

如果你嘗試跨磁碟區移動檔案,且該檔案正在使用中,該檔案會被複製到目的地,但不會從來源刪除。

關於常見 I/O 任務的清單,請參見 Common 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 未被找到。

sourceFileNamedestFileNamenull

.NET Framework 與 .NET Core 版本早於 2.1: sourceFileNamedestFileName 為零長度字串、僅含空白或無效字元。 你可以用這個 GetInvalidPathChars() 方法查詢無效字元。

來電者沒有所需的權限。

-或-

作業系統未能取得對目標檔案的專屬存取權。

指定的路徑、檔名或兩者都超過系統定義的最大長度。

destFileName 中指定的sourceFileName路徑無效(例如,它位於未映射的磁碟機上)。

sourceFileNamedestFileName 格式無效。

範例

以下範例移動一個檔案。

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

備註

此方法跨磁碟磁碟區運作,且若來源與目的地相同,則不會拋出例外。

sourceFileNamedestFileName參數可包含相對或絕對路徑資訊。 相對路徑資訊會被解讀為相對於目前工作目錄的相對於。 欲取得目前的工作目錄,請參見 GetCurrentDirectory

跨磁碟區移動檔案等同於複製檔案後,若複製成功,則從來源刪除。

如果你嘗試跨磁碟區移動檔案,且該檔案正在使用中,該檔案會被複製到目的地,但不會從來源刪除。

關於常見 I/O 任務的清單,請參見 Common I/O 任務

另請參閱

適用於