Recipe 12.20. Sending a File to the Recycle Bin


Problem

You need to delete a file or, even better, send it to the Recycle Bin.

Solution

The My.Computer. FileSystem. DeleteFile( ) method allows you to either permanently delete a file or send it to the Recycle Bin. The basic syntax is:

 My.Computer.FileSystem.DeleteFile(filePath _    [, showUI [, recycle [, onUserCancel]]] ) 

To send the file to the Recycle Bin, the recycle option needs to be set appropriately:

 My.Computer.FileSystem.DeleteFile(filePath, _    UIOption.OnlyErrorDialogs, _    RecycleOption.SendToRecycleBin) 

Discussion

The first DeleteFile() argument accepts a single file to be deleted, and you can include up to three additional optional arguments: showUI (which impacts user presentation during the deletion), recycle (which indicates whether or not to use the Recycle Bin), and onUserCancel (which sets what happens when the user aborts the deletion).

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


UIOption.AllDialogs

An animated progress dialog appears during the file deletion to indicate the current status during that operation. (The dialog might not appear for deletes 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 delete takes. This is the default method.

The recycle argument accepts one of the following Microsoft.VisualBasic.FileIO.RecycleOption enumeration values:


RecycleOption.DeletePermanently

The file is immediately and permanently removed from the disk.


RecycleOption.SendToRecycleBin

Instead of deleting the file, DeleteFile( ) moves the file to the Recycle Bin pseudodirectory.

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


UICancelOption.DoNothing

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


UICancelOption.ThrowException

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




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