FileSystem.CopyDirectory 方法

定义

将目录内容复制到其他目录。Copies the contents of a directory to another directory.

重载

CopyDirectory(String, String)

将目录内容复制到其他目录。Copies the contents of a directory to another directory.

CopyDirectory(String, String, UIOption)

将目录内容复制到其他目录。Copies the contents of a directory to another directory.

CopyDirectory(String, String, Boolean)

将目录内容复制到其他目录。Copies the contents of a directory to another directory.

CopyDirectory(String, String, UIOption, UICancelOption)

将目录内容复制到其他目录。Copies the contents of a directory to another directory.

CopyDirectory(String, String)

将目录内容复制到其他目录。Copies the contents of a directory to another directory.

public:
 static void CopyDirectory(System::String ^ sourceDirectoryName, System::String ^ destinationDirectoryName);
public static void CopyDirectory (string sourceDirectoryName, string destinationDirectoryName);
static member CopyDirectory : string * string -> unit
Public Shared Sub CopyDirectory (sourceDirectoryName As String, destinationDirectoryName As String)

参数

sourceDirectoryName
String

要复制的目录。The directory to be copied.

destinationDirectoryName
String

应将目录内容复制到的位置。The location to which the directory contents should be copied.

例外

路径由于以下原因之一无效:是零长度字符串;仅为空白;包含无效字符;是一个设备路径(以 \\.\ 开头)。The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\).

destinationDirectoryNamesourceDirectoryNameNothing 或空字符串。destinationDirectoryName or sourceDirectoryName is Nothing or an empty string.

源目录不存在。The source directory does not exist.

源路径和目标路径相同。The source path and target path are the same.

操作为循环。The operation is cyclic.

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

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

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

目标文件存在但无法访问。A destination file exists but cannot be accessed.

示例

下面的示例将目录的内容复制 TestDirectory1 到中 TestDirectory2The following example copies the contents of directory TestDirectory1 into TestDirectory2.

My.Computer.FileSystem.CopyDirectory("C:\TestDirectory1", "C:\TestDirectory2")

将和替换为要 C:\TestDirectory1 C:\TestDirectory2 复制的目录的路径和名称,将替换为要复制到的位置。Replace C:\TestDirectory1 and C:\TestDirectory2 with the path and name of the directory you wish to copy and the location to which you wish to copy it.

注解

此方法将源目录的内容复制到目标目录。This method copies the contents of the source directory to the target directory. 如果目标目录不存在,则创建它。If the target directory does not exist, it is created. 如果目标位置中存在具有相同名称的目录,则将合并这两个目录的内容。If a directory with the same name exists in the target location, the contents of the two directories are merged. 操作期间可为此目录指定新名称。You can specify a new name for the directory during the operation.

在目录中复制文件时,可能会引发由特定文件导致的异常。When copying files within a directory, exceptions may be thrown that are caused by a specific file. 如果引发此类异常,则会将它们合并为一个异常,其属性以的 Data 形式保存项, IDictionary 其中文件或目录路径为密钥,特定异常消息包含在对应的值中。When such exceptions are thrown, they are consolidated into a single exception whose Data property holds entries in the form of an IDictionary in which the file or directory path is the key and the specific exception message is contained in the corresponding value. 用于 For…Each 枚举条目。Use For…Each to enumerate through the entries.

下表列出了涉及方法的任务的示例 My.Computer.FileSystem.CopyDirectoryThe following table lists an example of a task involving the My.Computer.FileSystem.CopyDirectory method.

功能To 查看See
复制目录Copy a directory 如何:在 Visual Basic 中将一个目录复制到另一个目录How to: Copy a Directory to Another Directory in Visual Basic

另请参阅

适用于

CopyDirectory(String, String, UIOption)

将目录内容复制到其他目录。Copies the contents of a directory to another directory.

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

参数

sourceDirectoryName
String

要复制的目录。The directory to be copied.

destinationDirectoryName
String

应将目录内容复制到的位置。The location to which the directory contents should be copied.

showUI
UIOption

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

例外

路径由于以下原因之一无效:是零长度字符串;仅为空白;包含无效字符;是一个设备路径(以 \\.\ 开头)。The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\).

destinationDirectoryNamesourceDirectoryNameNothing 或空字符串。destinationDirectoryName or sourceDirectoryName is Nothing or an empty string.

源目录不存在。The source directory does not exist.

源路径和目标路径相同。The source path and target path are the same.

操作为循环。The operation is cyclic.

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

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

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

目标文件存在但无法访问。A destination file exists but cannot be accessed.

ShowUI 设置为 UIOption.AllDialogs 且用户取消了该操作,或者无法复制目录中的一个或多个文件。ShowUI is set to UIOption.AllDialogs and the user cancels the operation, or one or more files in the directory cannot be copied.

示例

下面的示例将目录的内容复制 TestDirectory1TestDirectory2 ,并显示进度对话框和任何错误对话框。The following example copies the contents of directory TestDirectory1 into TestDirectory2, showing progress dialog and any error dialog.

My.Computer.FileSystem.CopyDirectory("C:\TestDirectory1", "C:\TestDirectory2", UIOption.AllDialogs)

将和替换为要 C:\TestDirectory1 C:\TestDirectory2 复制的目录的路径和名称,将替换为要复制到的位置。Replace C:\TestDirectory1 and C:\TestDirectory2 with the path and name of the directory you wish to copy and the location to which you wish to copy it.

注解

此方法将源目录的内容复制到目标目录。This method copies the contents of the source directory to the target directory. 如果目标目录不存在,则创建它。If the target directory does not exist, it is created. 如果目标位置中存在具有相同名称的目录,则将合并这两个目录的内容。If a directory with the same name exists in the target location, the contents of the two directories are merged. 操作期间可为此目录指定新名称。You can specify a new name for the directory during the operation.

在目录中复制文件时,可能会引发由特定文件导致的异常。When copying files within a directory, exceptions may be thrown that are caused by a specific file. 如果引发此类异常,则会将它们合并为一个异常,其属性以的 Data 形式保存项, IDictionary 其中文件或目录路径为密钥,特定异常消息包含在对应的值中。When such exceptions are thrown, they are consolidated into a single exception whose Data property holds entries in the form of an IDictionary in which the file or directory path is the key and the specific exception message is contained in the corresponding value. 用于 For…Each 枚举条目。Use For…Each to enumerate through the entries.

下表列出了涉及方法的任务的示例 My.Computer.FileSystem.CopyDirectoryThe following table lists an example of a task involving the My.Computer.FileSystem.CopyDirectory method.

功能To 查看See
复制目录Copy a directory 如何:在 Visual Basic 中将一个目录复制到另一个目录How to: Copy a Directory to Another Directory in Visual Basic

另请参阅

适用于

CopyDirectory(String, String, Boolean)

将目录内容复制到其他目录。Copies the contents of a directory to another directory.

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

参数

sourceDirectoryName
String

要复制的目录。The directory to be copied.

destinationDirectoryName
String

应将目录内容复制到的位置。The location to which the directory contents should be copied.

overwrite
Boolean

要覆盖现有文件,则为 True;否则为 FalseTrue to overwrite existing files; otherwise False. 默认值为 FalseDefault is False.

例外

路径由于以下原因之一无效:是零长度字符串;仅为空白;包含无效字符;是一个设备路径(以 \\.\ 开头)。The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\).

destinationDirectoryNamesourceDirectoryNameNothing 或空字符串。destinationDirectoryName or sourceDirectoryName is Nothing or an empty string.

源目录不存在。The source directory does not exist.

源路径和目标路径相同。The source path and target path are the same.

操作为循环。The operation is cyclic.

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

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

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

目标文件存在但无法访问。A destination file exists but cannot be accessed.

示例

下面的示例将目录的内容复制 TestDirectory1TestDirectory2 中,并覆盖现有文件。The following example copies the contents of directory TestDirectory1 into TestDirectory2, overwriting existing files.

My.Computer.FileSystem.CopyDirectory("C:\TestDirectory1", "C:\TestDirectory2", True)

将和替换为要 C:\TestDirectory1 C:\TestDirectory2 复制的目录的路径和名称,将替换为要复制到的位置。Replace C:\TestDirectory1 and C:\TestDirectory2 with the path and name of the directory you wish to copy and the location to which you wish to copy it.

注解

此方法将源目录的内容复制到目标目录。This method copies the contents of the source directory to the target directory. 如果目标目录不存在,则创建它。If the target directory does not exist, it is created. 如果目标位置中存在具有相同名称的目录,则将合并这两个目录的内容。If a directory with the same name exists in the target location, the contents of the two directories are merged. 操作期间可为此目录指定新名称。You can specify a new name for the directory during the operation.

在目录中复制文件时,可能会引发由特定文件导致的异常,例如,在将设置为时,在合并期间存在的文件 overwrite FalseWhen copying files within a directory, exceptions may be thrown that are caused by a specific file, such as a file existing during a merge while overwrite is set to False. 如果引发此类异常,则会将它们合并为一个异常,其属性以的 Data 形式保存项, IDictionary 其中文件或目录路径为密钥,特定异常消息包含在对应的值中。When such exceptions are thrown, they are consolidated into a single exception whose Data property holds entries in the form of an IDictionary in which the file or directory path is the key and the specific exception message is contained in the corresponding value. 用于 For…Each 枚举条目。Use For…Each to enumerate through the entries.

下表列出了涉及方法的任务的示例 My.Computer.FileSystem.CopyDirectoryThe following table lists an example of a task involving the My.Computer.FileSystem.CopyDirectory method.

功能To 查看See
复制目录Copy a directory 如何:在 Visual Basic 中将一个目录复制到另一个目录How to: Copy a Directory to Another Directory in Visual Basic

另请参阅

适用于

CopyDirectory(String, String, UIOption, UICancelOption)

将目录内容复制到其他目录。Copies the contents of a directory to another directory.

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

参数

sourceDirectoryName
String

要复制的目录。The directory to be copied.

destinationDirectoryName
String

应将目录内容复制到的位置。The location to which the directory contents 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.

例外

路径由于以下原因之一无效:是零长度字符串;仅为空白;包含无效字符;是一个设备路径(以 \\.\ 开头)。The path is not valid for one of the following reasons: it is a zero-length string; it contains only white space; it contains invalid characters; or it is a device path (starts with \\.\).

destinationDirectoryNamesourceDirectoryNameNothing 或空字符串。destinationDirectoryName or sourceDirectoryName is Nothing or an empty string.

源目录不存在。The source directory does not exist.

源路径和目标路径相同。The source path and target path are the same.

操作为循环。The operation is cyclic.

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

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

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

目标文件存在但无法访问。A destination file exists but cannot be accessed.

ShowUI 设置为 UIOption.AllDialogs 且用户取消了该操作,或者无法复制目录中的一个或多个文件。ShowUI is set to UIOption.AllDialogs and the user cancels the operation, or one or more files in the directory cannot be copied.

示例

下面的示例将目录的内容复制 TestDirectory1TestDirectory2 中,显示进度对话框和任何错误对话框,如果用户在操作过程中单击 " 取消 ",则不执行任何操作。The following example copies the contents of directory TestDirectory1 into TestDirectory2, showing progress dialog and any error dialog, and does nothing if the user clicks Cancel during the operation.

My.Computer.FileSystem.CopyDirectory("C:\TestDirectory1", "C:\TestDirectory2", UIOption.AllDialogs, UICancelOption.DoNothing)

将和替换为要 C:\TestDirectory1 C:\TestDirectory2 复制的目录的路径和名称,将替换为要复制到的位置。Replace C:\TestDirectory1 and C:\TestDirectory2 with the path and name of the directory you wish to copy and the location to which you wish to copy it.

注解

此方法将源目录的内容复制到目标目录。This method copies the contents of the source directory to the target directory. 如果目标目录不存在,则创建它。If the target directory does not exist, it is created. 如果目标位置中存在具有相同名称的目录,则将合并这两个目录的内容。If a directory with the same name exists in the target location, the contents of the two directories are merged. 操作期间可为此目录指定新名称。You can specify a new name for the directory during the operation.

在目录中复制文件时,可能会引发由特定文件导致的异常。When copying files within a directory, exceptions may be thrown that are caused by a specific file. 如果引发此类异常,则会将它们合并为一个异常,其属性以的 Data 形式保存项, IDictionary 其中文件或目录路径为密钥,特定异常消息包含在对应的值中。When such exceptions are thrown, they are consolidated into a single exception whose Data property holds entries in the form of an IDictionary in which the file or directory path is the key and the specific exception message is contained in the corresponding value. 用于 For…Each 枚举条目。Use For…Each to enumerate through the entries.

下表列出了涉及方法的任务的示例 My.Computer.FileSystem.CopyDirectoryThe following table lists an example of a task involving the My.Computer.FileSystem.CopyDirectory method.

功能To 查看See
复制目录Copy a directory 如何:在 Visual Basic 中将一个目录复制到另一个目录How to: Copy a Directory to Another Directory in Visual Basic

另请参阅

适用于