Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

FileSystem.CopyDirectory Method (String, String, UIOption, UICancelOption)

Copies the contents of a directory to another directory.

Namespace:  Microsoft.VisualBasic.FileIO
Assembly:  Microsoft.VisualBasic (in Microsoft.VisualBasic.dll)
public static void CopyDirectory(
	string sourceDirectoryName,
	string destinationDirectoryName,
	UIOption showUI,
	UICancelOption onUserCancel
)

Parameters

sourceDirectoryName
Type: System.String

The directory to be copied.

destinationDirectoryName
Type: System.String

The location to which the directory contents should be copied.

showUI
Type: Microsoft.VisualBasic.FileIO.UIOption

Whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs.

onUserCancel
Type: Microsoft.VisualBasic.FileIO.UICancelOption

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

ExceptionCondition
ArgumentException

The new name specified for the directory contains a colon (:) or slash (\ or /).

ArgumentException

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

ArgumentNullException

destinationDirectoryName or sourceDirectoryName is Nothing or an empty string.

DirectoryNotFoundException

The source directory does not exist.

IOException

The source directory is a root directory

IOException

The combined path points to an existing file.

IOException

The source path and target path are the same.

InvalidOperationException

The operation is cyclic.

PathTooLongException

The path exceeds the system-defined maximum length.

NotSupportedException

A folder name in the path contains a colon (:) or is in an invalid format.

SecurityException

The user lacks necessary permissions to view the path.

UnauthorizedAccessException

A destination file exists but cannot be accessed.

OperationCanceledException

ShowUI is set to UIOption.AllDialogs and the user cancels the operation, or one or more files in the directory cannot be copied.

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.

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.

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.