FileSystem.MoveFile 方法

定義

將檔案移到新位置。

多載

名稱 Description
MoveFile(String, String)

將檔案移到新位置。

MoveFile(String, String, UIOption)

將檔案移到新位置。

MoveFile(String, String, Boolean)

將檔案移到新位置。

MoveFile(String, String, UIOption, UICancelOption)

將檔案移到新位置。

MoveFile(String, String)

將檔案移到新位置。

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

參數

sourceFileName
String

檔案的路徑。

destinationFileName
String

檔案應該移入的目錄路徑。

例外狀況

該路徑不成立,原因之一是:它是零長度字串;它僅包含空白;它包含無效字元;或是裝置路徑(以 \\.\ 開頭);結尾是拖尾的斜線。

destinationFileNameNothing 空字串。

原始檔案無效或不存在。

該檔案被其他程序使用,或發生 I/O 錯誤。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱包含冒號(:),或格式無效。

使用者缺乏必要的權限來查看路徑。

範例

這個範例會將名稱為 Test.txt 的檔案從 TestDir1 移至。TestDir2

My.Computer.FileSystem.MoveFile("C:\TestDir1\test.txt", "C:\TestDir2\test.txt")

此範例將檔案Test.txtTestDir1從 移動到 TestDir2 並重新命名 Test2.txt

My.Computer.FileSystem.MoveFile("C:\TestDir1\test.txt", "C:\TestDir2\test2.txt")

備註

如果目標結構不存在,則會被創建。

MoveFile 方法僅在相同磁碟區移動檔案時保留 ACE(存取控制 條目)。 這包括繼承的ACE,移動時會直接成為ACE(直接ACE優先於繼承的ACE)。 若檔案在不同卷間移動,ACE 將不會被複製。

下表列出一個涉及此 My.Computer.FileSystem.MoveFile 方法的任務範例。

看!
移動檔案 如何:在 Visual Basic

適用於

MoveFile(String, String, UIOption)

將檔案移到新位置。

public:
 static void MoveFile(System::String ^ sourceFileName, System::String ^ destinationFileName, Microsoft::VisualBasic::FileIO::UIOption showUI);
public static void MoveFile(string sourceFileName, string destinationFileName, Microsoft.VisualBasic.FileIO.UIOption showUI);
static member MoveFile : string * string * Microsoft.VisualBasic.FileIO.UIOption -> unit
Public Shared Sub MoveFile (sourceFileName As String, destinationFileName As String, showUI As UIOption)

參數

sourceFileName
String

檔案的路徑。

destinationFileName
String

檔案應該移入的目錄路徑。

showUI
UIOption

規定是否要以視覺方式追蹤作業進度。 預設值為 UIOption.OnlyErrorDialogs

例外狀況

該路徑不成立,原因之一是:它是零長度字串;它僅包含空白;它包含無效字元;或是裝置路徑(以 \\.\ 開頭);結尾是拖尾的斜線。

destinationFileNameNothing 空字串。

原始檔案無效或不存在。

該檔案被其他程序使用,或發生 I/O 錯誤。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱包含冒號(:),或格式無效。

使用者缺乏必要的權限來查看路徑。

範例

這個範例會將名稱為 Test.txt 的檔案從 TestDir1 移至。TestDir2

My.Computer.FileSystem.MoveFile("C:\TestDir1\test.txt", "C:\TestDir2\test.txt")

此範例將檔案Test.txtTestDir1從 移動到 TestDir2 並重新命名 Test2.txt

My.Computer.FileSystem.MoveFile("C:\TestDir1\test.txt", "C:\TestDir2\test2.txt")

備註

如果目標結構不存在,則會被創建。

MoveFile 方法僅在相同磁碟區移動檔案時保留 ACE(存取控制 條目)。 這包括繼承的ACE,移動時會直接成為ACE(直接ACE優先於繼承的ACE)。 若檔案在不同卷間移動,ACE 將不會被複製。

下表列出一個涉及此 My.Computer.FileSystem.MoveFile 方法的任務範例。

看!
移動檔案 如何:在 Visual Basic

另請參閱

適用於

MoveFile(String, String, Boolean)

將檔案移到新位置。

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

參數

sourceFileName
String

檔案的路徑。

destinationFileName
String

檔案應該移入的目錄路徑。

overwrite
Boolean

True 覆寫現有檔案;否則 False。 預設值為 False

例外狀況

該路徑不成立,原因之一是:它是零長度字串;它僅包含空白;它包含無效字元;或是裝置路徑(以 \\.\ 開頭);結尾是拖尾的斜線。

destinationFileNameNothing 空字串。

原始檔案無效或不存在。

該檔案被其他程序使用,或發生 I/O 錯誤。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱包含冒號(:),或格式無效。

使用者缺乏必要的權限來查看路徑。

範例

這個範例會將名稱為 Test.txt 的檔案從 TestDir1 移至。TestDir2

My.Computer.FileSystem.MoveFile("C:\TestDir1\test.txt", "C:\TestDir2\test.txt")

此範例將檔案Test.txtTestDir1從 移動到 TestDir2 並重新命名 Test2.txt

My.Computer.FileSystem.MoveFile("C:\TestDir1\test.txt", "C:\TestDir2\test2.txt")

備註

如果目標結構不存在,則會被創建。

MoveFile 方法僅在相同磁碟區移動檔案時保留 ACE(存取控制 條目)。 這包括繼承的ACE,移動時會直接成為ACE(直接ACE優先於繼承的ACE)。 若檔案在不同卷間移動,ACE 將不會被複製。

下表列出一個涉及此 My.Computer.FileSystem.MoveFile 方法的任務範例。

看!
移動檔案 如何:在 Visual Basic

適用於

MoveFile(String, String, UIOption, UICancelOption)

將檔案移到新位置。

public:
 static void MoveFile(System::String ^ sourceFileName, System::String ^ destinationFileName, Microsoft::VisualBasic::FileIO::UIOption showUI, Microsoft::VisualBasic::FileIO::UICancelOption onUserCancel);
public static void MoveFile(string sourceFileName, string destinationFileName, Microsoft.VisualBasic.FileIO.UIOption showUI, Microsoft.VisualBasic.FileIO.UICancelOption onUserCancel);
static member MoveFile : string * string * Microsoft.VisualBasic.FileIO.UIOption * Microsoft.VisualBasic.FileIO.UICancelOption -> unit
Public Shared Sub MoveFile (sourceFileName As String, destinationFileName As String, showUI As UIOption, onUserCancel As UICancelOption)

參數

sourceFileName
String

檔案的路徑。

destinationFileName
String

檔案應該移入的目錄路徑。

showUI
UIOption

規定是否要以視覺方式追蹤作業進度。 預設值為 UIOption.OnlyErrorDialogs

onUserCancel
UICancelOption

指定當使用者取消操作時,是否會拋出例外。 預設值為 UICancelOption.ThrowException

例外狀況

該路徑不成立,原因之一是:它是零長度字串;它僅包含空白;它包含無效字元;或是裝置路徑(以 \\.\ 開頭);結尾是拖尾的斜線。

destinationFileNameNothing 空字串。

原始檔案無效或不存在。

該檔案被其他程序使用,或發生 I/O 錯誤。

onUserCancel 設定為 ThrowException,則使用者要麼取消操作,要麼發生未說明的 I/O 錯誤。

路徑超過系統定義的最大長度。

路徑中的檔案或目錄名稱包含冒號(:),或格式無效。

使用者缺乏必要的權限來查看路徑。

範例

這個範例會將名稱為 Test.txt 的檔案從 TestDir1 移至。TestDir2

My.Computer.FileSystem.MoveFile("C:\TestDir1\test.txt", "C:\TestDir2\test.txt")

此範例將檔案Test.txtTestDir1從 移動到 TestDir2 並重新命名 Test2.txt

My.Computer.FileSystem.MoveFile("C:\TestDir1\test.txt", "C:\TestDir2\test2.txt")

備註

如果目標結構不存在,則會被創建。

MoveFile 方法僅在相同磁碟區移動檔案時保留 ACE(存取控制 條目)。 這包括繼承的ACE,移動時會直接成為ACE(直接ACE優先於繼承的ACE)。 若檔案在不同卷間移動,ACE 將不會被複製。

下表列出一個涉及此 My.Computer.FileSystem.MoveFile 方法的任務範例。

看!
移動檔案 如何:在 Visual Basic

另請參閱

適用於