FileSystem.CopyFile 方法

定义

将文件复制到新的位置。Copies a file to a new location.

重载

CopyFile(String, String)

将文件复制到新的位置。Copies a file to a new location.

CopyFile(String, String, UIOption)

将文件复制到新的位置。Copies a file to a new location.

CopyFile(String, String, Boolean)

将文件复制到新的位置。Copies a file to a new location.

CopyFile(String, String, UIOption, UICancelOption)

将文件复制到新的位置。Copies a file to a new location.

CopyFile(String, String)

将文件复制到新的位置。Copies a file to a new location.

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

参数

sourceFileName
String

要复制的文件。The file to be copied.

destinationFileName
String

文件应复制到的位置。The location to which the file should be copied.

异常

destinationFileName 包含路径信息。destinationFileName contains path information.

destinationFileNamesourceFileNameNothing 或空字符串。destinationFileName or sourceFileName is Nothing or an empty string.

源文件无效或不存在。The source file is not valid or does not exist.

目标目录中的同名文件正被使用。A file in the target directory with the same name is in use.

路径中的文件名或目录名包含冒号 (:),或格式无效。A file or directory name in the path contains a colon (:) or is in an invalid format.

路径超过了系统定义的最大长度。The path exceeds the system-defined maximum length.

该用户没有必需的权限。The user does not have required permission.

该用户缺少查看该路径所必需的权限。The user lacks necessary permissions to view the path.

示例

此示例将文件 Test.txt 复制到目录 TestFiles2 而不覆盖现有文件。This example copies the file Test.txt to the directory TestFiles2 without overwriting existing files.

My.Computer.FileSystem.CopyFile( _
    "C:\UserFiles\TestFiles\test.txt", _
    "C:\UserFiles\TestFiles2\test.txt", overwrite:=False)

将文件路径替换为要在代码中使用的路径。Replace the file paths with the paths you want to use in your code.

此示例将文件 Test.txt 复制到目录 TestFiles2 并将其重命名 NewFile.txtThis example copies the file Test.txt to the directory TestFiles2 and renames it NewFile.txt.

My.Computer.FileSystem.CopyFile( _
    "C:\UserFiles\TestFiles\test.txt", _
    "C:\UserFiles\TestFiles2\NewFile.txt", _
    FileIO.UIOption.OnlyErrorDialogs, _
    FileIO.UICancelOption.DoNothing)

将文件路径替换为要在代码中使用的路径。Replace the file paths with the paths you want to use in your code.

注解

CopyFile 不会保留 Ace (访问控制项)。CopyFile does not preserve ACEs (Access Control Entries). 新创建的文件从创建它的目录继承默认的 Ace。The newly created file inherits default ACEs from the directory in which it is created.

下表列出了涉及 My.Computer.FileSystem.CopyFile 方法的任务示例。The following table lists examples of tasks involving the My.Computer.FileSystem.CopyFile method.

To 请参阅See
将文件复制到相同的目录。Copy a file to the same directory. 如何:在 Visual Basic 中的同一目录中创建文件的副本How to: Create a Copy of a File in the Same Directory in Visual Basic
将文件复制到其他目录。Copy a file to a different directory. 如何:在 Visual Basic 中的其他目录中创建文件的副本How to: Create a Copy of a File in a Different Directory in Visual Basic

另请参阅

CopyFile(String, String, UIOption)

将文件复制到新的位置。Copies a file to a new location.

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

参数

sourceFileName
String

要复制的文件。The file to be copied.

destinationFileName
String

文件应复制到的位置。The location to which the file should be copied.

showUI
UIOption

是否对操作进度进行可视跟踪。Whether to visually track the operation's progress. 默认值为 UIOption.OnlyErrorDialogsDefault is UIOption.OnlyErrorDialogs.

异常

destinationFileName 包含路径信息。destinationFileName contains path information.

destinationFileNamesourceFileNameNothing 或空字符串。destinationFileName or sourceFileName is Nothing or an empty string.

源文件无效或不存在。The source file is not valid or does not exist.

目标文件存在,并且 overwrite 设置为 FalseThe destination file exists and overwrite is set to False.

路径中的文件名或目录名包含冒号 (:),或格式无效。A file or directory name in the path contains a colon (:) or is in an invalid format.

路径超过了系统定义的最大长度。The path exceeds the system-defined maximum length.

该用户没有必需的权限。The user does not have required permission.

该用户缺少查看该路径所必需的权限。The user lacks necessary permissions to view the path.

示例

此示例将文件 Test.txt 复制到目录 TestFiles2 而不覆盖现有文件。This example copies the file Test.txt to the directory TestFiles2 without overwriting existing files.

My.Computer.FileSystem.CopyFile( _
    "C:\UserFiles\TestFiles\test.txt", _
    "C:\UserFiles\TestFiles2\test.txt", overwrite:=False)

将文件路径替换为要在代码中使用的路径。Replace the file paths with the paths you want to use in your code.

此示例将文件 Test.txt 复制到目录 TestFiles2 并将其重命名 NewFile.txtThis example copies the file Test.txt to the directory TestFiles2 and renames it NewFile.txt.

My.Computer.FileSystem.CopyFile( _
    "C:\UserFiles\TestFiles\test.txt", _
    "C:\UserFiles\TestFiles2\NewFile.txt", _
    FileIO.UIOption.OnlyErrorDialogs, _
    FileIO.UICancelOption.DoNothing)

将文件路径替换为要在代码中使用的路径。Replace the file paths with the paths you want to use in your code.

注解

CopyFile 不会保留 Ace (访问控制项)。CopyFile does not preserve ACEs (Access Control Entries). 新创建的文件从创建它的目录继承默认的 Ace。The newly created file inherits default ACEs from the directory in which it is created.

下表列出了涉及 My.Computer.FileSystem.CopyFile 方法的任务示例。The following table lists examples of tasks involving the My.Computer.FileSystem.CopyFile method.

To 请参阅See
将文件复制到相同的目录。Copy a file to the same directory. 如何:在 Visual Basic 中的同一目录中创建文件的副本How to: Create a Copy of a File in the Same Directory in Visual Basic
将文件复制到其他目录。Copy a file to a different directory. 如何:在 Visual Basic 中的其他目录中创建文件的副本How to: Create a Copy of a File in a Different Directory in Visual Basic

另请参阅

CopyFile(String, String, Boolean)

将文件复制到新的位置。Copies a file to a new location.

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

参数

sourceFileName
String

要复制的文件。The file to be copied.

destinationFileName
String

文件应复制到的位置。The location to which the file should be copied.

overwrite
Boolean

如果应覆盖现有文件,则为 True;否则为 FalseTrue if existing files should be overwritten; otherwise False. 默认值为 FalseDefault is False.

异常

destinationFileName 包含路径信息。destinationFileName contains path information.

destinationFileNamesourceFileNameNothing 或空字符串。destinationFileName or sourceFileName is Nothing or an empty string.

源文件无效或不存在。The source file is not valid or does not exist.

目标文件存在,并且 overwrite 设置为 FalseThe destination file exists and overwrite is set to False.

路径中的文件名或目录名包含冒号 (:),或格式无效。A file or directory name in the path contains a colon (:) or is in an invalid format.

路径超过了系统定义的最大长度。The path exceeds the system-defined maximum length.

该用户没有必需的权限。The user does not have required permission.

该用户缺少查看该路径所必需的权限。The user lacks necessary permissions to view the path.

示例

此示例将文件 Test.txt 复制到目录 TestFiles2 而不覆盖现有文件。This example copies the file Test.txt to the directory TestFiles2 without overwriting existing files.

My.Computer.FileSystem.CopyFile( _
    "C:\UserFiles\TestFiles\test.txt", _
    "C:\UserFiles\TestFiles2\test.txt", overwrite:=False)

将文件路径替换为要在代码中使用的路径。Replace the file paths with the paths you want to use in your code.

此示例将文件 Test.txt 复制到目录 TestFiles2 并将其重命名 NewFile.txtThis example copies the file Test.txt to the directory TestFiles2 and renames it NewFile.txt.

My.Computer.FileSystem.CopyFile( _
    "C:\UserFiles\TestFiles\test.txt", _
    "C:\UserFiles\TestFiles2\NewFile.txt", _
    FileIO.UIOption.OnlyErrorDialogs, _
    FileIO.UICancelOption.DoNothing)

将文件路径替换为要在代码中使用的路径。Replace the file paths with the paths you want to use in your code.

注解

CopyFile 不会保留 Ace (访问控制项)。CopyFile does not preserve ACEs (Access Control Entries). 新创建的文件从创建它的目录继承默认的 Ace。The newly created file inherits default ACEs from the directory in which it is created.

下表列出了涉及 My.Computer.FileSystem.CopyFile 方法的任务示例。The following table lists examples of tasks involving the My.Computer.FileSystem.CopyFile method.

To 请参阅See
将文件复制到相同的目录。Copy a file to the same directory. 如何:在 Visual Basic 中的同一目录中创建文件的副本How to: Create a Copy of a File in the Same Directory in Visual Basic
将文件复制到其他目录。Copy a file to a different directory. 如何:在 Visual Basic 中的其他目录中创建文件的副本How to: Create a Copy of a File in a Different Directory in Visual Basic

另请参阅

CopyFile(String, String, UIOption, UICancelOption)

将文件复制到新的位置。Copies a file to a new location.

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

参数

sourceFileName
String

要复制的文件。The file to be copied.

destinationFileName
String

文件应复制到的位置。The location to which the file should be copied.

showUI
UIOption

是否对操作进度进行可视跟踪。Whether to visually track the operation's progress. 默认值为 UIOption.OnlyErrorDialogsDefault is UIOption.OnlyErrorDialogs.

onUserCancel
UICancelOption

指定当用户在操作过程中单击“取消”时应执行的操作。Specifies what should be done if the user clicks Cancel during the operation. 默认值为 ThrowExceptionDefault is ThrowException.

异常

destinationFileName 包含路径信息。destinationFileName contains path information.

destinationFileNamesourceFileNameNothing 或空字符串。destinationFileName or sourceFileName is Nothing or an empty string.

源文件无效或不存在。The source file is not valid or does not exist.

目标文件存在,并且 overwrite 设置为 FalseThe destination file exists and overwrite is set to False.

路径中的文件名或目录名包含冒号 (:),或格式无效。A file or directory name in the path contains a colon (:) or is in an invalid format.

路径超过了系统定义的最大长度。The path exceeds the system-defined maximum length.

该用户没有必需的权限。The user does not have required permission.

该用户缺少查看该路径所必需的权限。The user lacks necessary permissions to view the path.

UICancelOption 设置为 ThrowException,并且用户已经取消了该操作或者发生了未指定的 I/O 错误。UICancelOption is set to ThrowException, and the user has canceled the operation or an unspecified I/O error occurs.

示例

此示例将文件 Test.txt 复制到目录 TestFiles2 而不覆盖现有文件。This example copies the file Test.txt to the directory TestFiles2 without overwriting existing files.

My.Computer.FileSystem.CopyFile( _
    "C:\UserFiles\TestFiles\test.txt", _
    "C:\UserFiles\TestFiles2\test.txt", overwrite:=False)

将文件路径替换为要在代码中使用的路径。Replace the file paths with the paths you want to use in your code.

此示例将文件 Test.txt 复制到目录 TestFiles2 并将其重命名 NewFile.txtThis example copies the file Test.txt to the directory TestFiles2 and renames it NewFile.txt.

My.Computer.FileSystem.CopyFile( _
    "C:\UserFiles\TestFiles\test.txt", _
    "C:\UserFiles\TestFiles2\NewFile.txt", _
    FileIO.UIOption.OnlyErrorDialogs, _
    FileIO.UICancelOption.DoNothing)

将文件路径替换为要在代码中使用的路径。Replace the file paths with the paths you want to use in your code.

注解

CopyFile 不会保留 Ace (访问控制项)。CopyFile does not preserve ACEs (Access Control Entries). 新创建的文件从创建它的目录继承默认的 Ace。The newly created file inherits default ACEs from the directory in which it is created.

下表列出了涉及 My.Computer.FileSystem.CopyFile 方法的任务示例。The following table lists examples of tasks involving the My.Computer.FileSystem.CopyFile method.

To 请参阅See
将文件复制到相同的目录。Copy a file to the same directory. 如何:在 Visual Basic 中的同一目录中创建文件的副本How to: Create a Copy of a File in the Same Directory in Visual Basic
将文件复制到其他目录。Copy a file to a different directory. 如何:在 Visual Basic 中的其他目录中创建文件的副本How to: Create a Copy of a File in a Different Directory in Visual Basic

另请参阅

适用于