My.Computer.FileSystem.CopyDirectory Method
Copies a directory to another directory.
' Usage
My.Computer.FileSystem.CopyDirectory(sourceDirectoryName ,destinationDirectoryName)
My.Computer.FileSystem.CopyDirectory(sourceDirectoryName ,destinationDirectoryName ,overwrite)
My.Computer.FileSystem.CopyDirectory(sourceDirectoryName ,destinationDirectoryName ,showUI)
My.Computer.FileSystem.CopyDirectory(sourceDirectoryName ,destinationDirectoryName ,showUI ,onUserCancel)
' Declaration
Public Sub CopyDirectory( _
ByVal sourceDirectoryName As String, _
ByVal destinationDirectoryName As String _
)
' -or-
Public Sub CopyDirectory( _
ByVal sourceDirectoryName As String, _
ByVal destinationDirectoryName As String, _
ByVal overwrite As Boolean _
)
' -or-
Public Sub CopyDirectory( _
ByVal sourceDirectoryName As String, _
ByVal destinationDirectoryName As String, _
ByVal showUI As UIOption _
)
' -or-
Public Sub CopyDirectory( _
ByVal sourceDirectoryName As String, _
ByVal destinationDirectoryName As String, _
ByVal showUI As UIOption, _
ByVal onUserCancel As UICancelOption _
)
Parameters
- sourceDirectoryName
String. The directory to be copied. Required.
- destinationDirectoryName
String. The location to which the directory should be copied. Required.
- overwrite
Boolean. Whether or not existing files should be overwritten. Default is False. Required.
- showUI
UIOption. Whether to visually track the operation's progress. Default is UIOption.OnlyErrorDialogs. Required.
- onUserCancel
UICancelOption. Specifies what should be done if the user clicks Cancel during the operation. Default is ThrowException. Required.
Exceptions
The following conditions may cause an exception:
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 \\.\) (ArgumentException).
The path is not valid because it is Nothing (ArgumentNullException).
destinationDirectoryName is Nothing or an empty string (ArgumentNullException)
The source directory does not exist (DirectoryNotFoundException).
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 (IOException).
ShowUI is set to UIOption.AllDialogs and the user cancels the operation, or one or more files in the directory cannot be copied (OperationCanceledException).
The operation is cyclic (InvalidOperationException).
The path contains a colon (:) (NotSupportedException).
The path exceeds the system-defined maximum length (PathTooLongException).
A file or folder name in the path contains a colon (:) or is in an invalid format (NotSupportedException).
The user lacks necessary permissions to view the path (SecurityException).
A destination file exists but cannot be accessed (UnauthorizedAccessException).
Remarks
This method copies the contents of the directory as well as the directory itself. 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.
Tasks
The following table lists examples of tasks involving the My.Computer.FileSystem.CopyDirectory method.
To | See |
---|---|
Copy a directory |
How to: Copy a Directory to Another Directory in Visual Basic |
Example
The following example copies the directoryTestDirectory1
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.
Requirements
Namespace: Microsoft.VisualBasic.MyServices
Class: FileSystemProxy (provides access to FileSystem)
Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)
Availability by Project Type
Project type | Available |
---|---|
Windows Application |
Yes |
Class Library |
Yes |
Console Application |
Yes |
Windows Control Library |
Yes |
Web Control Library |
Yes |
Windows Service |
Yes |
Web Site |
Yes |
Permissions
The following permissions may be necessary:
Permission | Description |
---|---|
Controls the ability to access files and folders. Associated enumeration: Unrestricted. |
|
Controls the permissions related to user interfaces and the clipboard. Associated enumeration: SafeSubWindows. |
For more information, see Code Access Security and Requesting Permissions.
See Also
Tasks
How to: Get the Collection of Files in a Directory in Visual Basic
How to: Move a Directory in Visual Basic
How to: Move the Contents of a Directory in Visual Basic
How to: Parse File Paths in Visual Basic
How to: Determine the Absolute Path of a File in Visual Basic
How to: Determine if a Directory Exists in Visual Basic