Recipe 12.19. Copying or Moving a File


You need to copy or move an existing file from one location to another.


Use the My.Computer.FileSystem. MoveFile() method to move a file from its current location to another place in the file system. Use the related CopyFile() method to copy the file instead of moving it. The basic syntax is:

 ' ---- To move the file. My.Computer.FileSystem.MoveFile( _    sourceFile, destFile[, overwriteFlag]) ' ---- To copy the file. My.Computer.FileSystem.CopyFile( _    sourceFile, destFile[, overwriteFlag]) 

Because destFile is a filename and not a directory name, you can effectively rename the file at the same time you move or copy it. When moving the file, you can keep the file in the same directory and just give it a new name, although using the RenameFile() method would be clearer. The optional overwriteFlag is a Boolean that indicates whether any existing file at the target should be replaced silently by the source file. It defaults to False.


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

 ' ----- MoveFile( ) syntax. My.Computer.  FileSystem.MoveFile( _    sourceFile, destFile, _    showUI [, onUserCancel]) ' ----- CopyFile( ) syntax. My.Computer.FileSystem.CopyFile( _    sourceFile, destFile, _    showUI [, onUserCancel]) 

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


An animated progress dialog appears during the file move or copy to indicate the current status during that operation. (The dialog might not appear for moves or 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 move or 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 move or copy operation includes a Cancel button. If the user clicks the Cancel button when the DoNothing option is in effect, the code calling MoveFile( ) or CopyFile( ) will not receive any notification that the move or copy was aborted early.


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

See Also

Recipes 12.4 and 12.5 show you how to move and copy whole directories instead of just files.

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: