แก้ไข

แชร์ผ่าน


FileSystem.CopyDirectory Method

Definition

Copies the contents of a directory to another directory.

Overloads

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)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

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)

Parameters

sourceDirectoryName
String

The directory to be copied.

destinationDirectoryName
String

The location to which the directory contents should be copied.

Exceptions

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 \\.\).

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.

Examples

The following example copies the contents of directory TestDirectory1 into TestDirectory2.

My.Computer.FileSystem.CopyDirectory("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.

Remarks

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. 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. Use For…Each to enumerate through the entries.

The following table lists an example of a task involving the My.Computer.FileSystem.CopyDirectory method.

To See
Copy a directory How to: Copy a Directory to Another Directory in Visual Basic

See also

Applies to

CopyDirectory(String, String, UIOption)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

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)

Parameters

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. Default is UIOption.OnlyErrorDialogs.

Exceptions

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 \\.\).

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 is set to UIOption.AllDialogs and the user cancels the operation, or one or more files in the directory cannot be copied.

Examples

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)

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.

Remarks

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. 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. Use For…Each to enumerate through the entries.

The following table lists an example of a task involving the My.Computer.FileSystem.CopyDirectory method.

To See
Copy a directory How to: Copy a Directory to Another Directory in Visual Basic

See also

Applies to

CopyDirectory(String, String, Boolean)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

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)

Parameters

sourceDirectoryName
String

The directory to be copied.

destinationDirectoryName
String

The location to which the directory contents should be copied.

overwrite
Boolean

True to overwrite existing files; otherwise False. Default is False.

Exceptions

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 \\.\).

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.

Examples

The following example copies the contents of directory TestDirectory1 into TestDirectory2, overwriting existing files.

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

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.

Remarks

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, such as a file existing during a merge while overwrite is set to False. 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. Use For…Each to enumerate through the entries.

The following table lists an example of a task involving the My.Computer.FileSystem.CopyDirectory method.

To See
Copy a directory How to: Copy a Directory to Another Directory in Visual Basic

See also

Applies to

CopyDirectory(String, String, UIOption, UICancelOption)

Source:
FileSystem.vb
Source:
FileSystem.vb
Source:
FileSystem.vb

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)

Parameters

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. Default is UIOption.OnlyErrorDialogs.

onUserCancel
UICancelOption

Specifies what should be done if the user clicks Cancel during the operation. Default is ThrowException.

Exceptions

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 \\.\).

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 is set to UIOption.AllDialogs and the user cancels the operation, or one or more files in the directory cannot be copied.

Examples

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)

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.

Remarks

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. 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. Use For…Each to enumerate through the entries.

The following table lists an example of a task involving the My.Computer.FileSystem.CopyDirectory method.

To See
Copy a directory How to: Copy a Directory to Another Directory in Visual Basic

See also

Applies to