共用方式為


File.Move 方法

定義

多載

Move(String, String)

移動指定的檔案至新的位置,提供指定新檔名的選項。

Move(String, String, Boolean)

將指定的檔案移至新位置,並提供指定新檔名的選項,並在目的地檔案已經存在時取代目的地檔案。

Move(String, String)

來源:
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 和 2.1 之前的 .NET Core 版本:sourceFileNamedestFileName 是長度為零的字串、只包含空格符,或包含無效的字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

呼叫端沒有必要的權限。

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

sourceFileNamedestFileName 指定的路徑無效 (例如,它位於未對應的磁碟機上)。

sourceFileNamedestFileName 格式無效。

範例

下列範例會移動檔案。

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 );
   }
}
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 和更新版本中,您可以呼叫 Move(String, String, Boolean) 將 參數 overwrite 設定為 true,如果檔案存在,則會取代檔案。

  • 在所有 .NET 版本中,您可以呼叫 Copy(String, String, Boolean) 以覆寫複製,然後呼叫 Delete 以移除多餘的原始程序檔。 如果複製的檔案很小,而且您正在尋找「不可部分完成」的檔案作業,建議使用此策略。 如果您 Delete 第一個檔案,且系統或程式損毀,目的地檔案將不再存在。

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

sourceFileNamedestFileName 自變數可以包含相對或絕對路徑資訊。 相對路徑資訊會解譯為相對於目前工作目錄。 若要取得目前的工作目錄,請參閱 GetCurrentDirectory

在磁碟區之間移動檔案相當於複製檔案,並在複製成功時從來源刪除檔案。

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

如需一般 I/O 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於

Move(String, String, Boolean)

來源:
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 和 2.1 之前的 .NET Core 版本:sourceFileNamedestFileName 是長度為零的字串、只包含空格符,或包含無效的字元。 您可以使用 GetInvalidPathChars() 方法查詢無效字元。

呼叫端沒有必要的權限。

-或-

操作系統無法取得目的地檔案的獨佔存取權。

指定的路徑、檔案名稱,或兩者都超出系統定義的長度上限。

sourceFileNamedestFileName 指定的路徑無效 (例如,它位於未對應的磁碟機上)。

sourceFileNamedestFileName 格式無效。

範例

下列範例會移動檔案。

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 );
   }
}
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 工作的清單,請參閱 一般 I/O 工作

另請參閱

適用於