ProblemYou need to copy the contents of an existing directory to a new location, leaving the original directory intact. SolutionUse the My.Computer.FileSystem.CopyDirectory() method to copy the contents of a directory from one place to another. DiscussionThe 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:
If you include the fourth onUserCancel argument, you provide it one of the following Microsoft.VisualBasic.UICancelOption enumeration values:
See AlsoSee Recipe 12.19 for details on copying individual files instead of whole directories. |