Recipe 12.4. Copying Directories


You need to copy the contents of an existing directory to a new location, leaving the original directory intact.


Use the My.Computer.FileSystem.CopyDirectory() method to copy the contents of a directory from one place to another.


The basic syntax of the CopyDirectory() method is:

 My.Computer.FileSystem.CopyDirectory( _    sourceDirectory, destDirectory) 

The sourceDirectory argument contains an absolute UNC-based or drive-letter-based path, or a relative path based on the current directory from the application's point of view. The destDirectory argument is also an absolute or relative path, although it may not appear hierarchically within the source directory. For example:

 My.Computer.FileSystem.CopyDirectory( _    "C:\WorkFiles", "C:\PlayFiles") 

The duplication of the source directory is complete, creating copies of all subordinate files and directories to any depth.

If the destination directory does not exist, CopyDirectory( ) creates it, including any nonexistent path components between the specified root and the final directory. If the destination directory is already present, any existing files at the destination remain intact, and new files are copied in amongst them, resulting in a merged destination directory. If one of the files to be copied already exists at the destination, CopyDirectory( ) generates an exception. If you want it to overwrite any matching files at the destination silently, use the optional third argumentthe overwrite argumentpassing a value of true:

 My.Computer.FileSystem.CopyDirectory( _   sourceDirectory, destDirectory, True) 

A variation of CopyDirectory( ) uses a different set of arguments to control the display of on-screen prompts and status notifications during the copy:

 My.Computer.FileSystem.CopyDirectory( _    sourceDirectory, destDirectory, _      showUI [, onUserCancel]) 

The showUI argument accepts one of the following Microsoft.VisualBasic.FileIO.UIOption enumeration values:


An animated progress dialog appears during the directory copy to indicate the current status as each file is copied. (The dialog might not appear for copies that involve a small amount of content.) Any errors that occur present their own separate error-dialog prompts.


While errors will appear through distinct error-dialog prompts, no animated status display appears, no matter how long the copy takes. This is the default method.

If you include the fourth onUserCancel argument, you provide it one of the following Microsoft.VisualBasic.UICancelOption enumeration values:


The on-screen status display during a copy operation includes a Cancel button. If the user clicks the Cancel button when the DoNothing option is in effect, the code calling CopyDirectory( ) will not receive any notification that the copy was aborted early.


If the user clicks the Cancel button on the directory copy status dialog when ThrowException is used, the CopyDirectory( ) method generates a System.IOException exception, which can be caught by the initiating code. This is the default method.

See Also

See Recipe 12.19 for details on copying individual files instead of whole directories.

Visual Basic 2005 Cookbook(c) Solutions for VB 2005 Programmers
Visual Basic 2005 Cookbook: Solutions for VB 2005 Programmers (Cookbooks (OReilly))
ISBN: 0596101775
EAN: 2147483647
Year: 2006
Pages: 400

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: