Recipe 12.5. Moving Directories


Problem

You need to move a directory from one location to another.

Solution

Use the My.Computer.FileSystem.MoveDirectory() method to relocate an existing directory from one place to another.

Discussion

The basic syntax of the MoveDirectory() method is:

 My.Computer.FileSystem.MoveDirectory( _    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.MoveDirectory( _    "C:\WorkFiles", "C:\PlayFiles") 

If the destination directory does not exist, MoveDirectory( ) creates it, including any nonexistent path components between the specified root and the final directory.

If all but the final directory component of the source and destination directories are the same, MoveDirectory() acts like a simple directory rename operation. The My.Computer.FileSystem.RenameDirectory() method may provide a clearer method of renaming directories within the same parent directory.

The movement of the source directory is complete, moving all subordinate files and directories to any depth. Also, you can move a directory between different logical disk drives.

The MoveDirectory() method creates the target directory if it does not yet exist. If the destination directory is already present, any existing files at the destination remain intact, and new files are moved in amongst them, resulting in a merged destination directory. If one of the files to be moved already exists at the destination, MoveDirectory() 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.MoveDirectory( _ sourceDirectory, destDirectory, True) 

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

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

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


UIOption.AllDialogs

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


UIOption.OnlyErrorDialogs

While errors will appear through distinct error-dialog prompts, no animated status display appears, no matter how long the move 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:


UICancelOption.DoNothing

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


UICancelOption.ThrowException

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

See Also

Recipe 12.4 shows how to copy an existing directory instead of moving it. To rename a directory without moving it to another parent directory, see Recipe 12.6.




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

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net