BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Input / Output
.NET 2.0+

Recycling Files and Folders Part 1

If your software can be used to delete files or folders, it can be advisable to move them to the Recycle Bin rather than permanently deleting them. This removes the problems caused by accidental deletion, as recycled files can be easily recovered.

The Recycle Bin

When files are deleted from disk in Microsoft Windows, they can either be permanently deleted or sent to the Recycle Bin. If permanently deleted, files can sometimes be retrieved using data recovery software. However, this process can fail if the area of disk originally used by the deleted file has subsequently been reused. If files or folders are sent to the Recycle Bin, they are simply marked as deleted and remain recoverable until the user empties the Recycle Bin.

When you develop software that deletes files or folders from disk, it can be useful to send them to the Recycle Bin instead of deleting them. For C# developers this can be achieved in two manners. Firstly, the .NET framework includes recycling methods within the Microsoft.VisualBasic assembly. Secondly, you can send items to the Recycle Bin using Platform Invocation Services (P/Invoke).

In the first instalment of this two-part article we will examine the methods provided by the Microsoft.VisualBasic assembly. This permits recycling files with purely managed code.

Sending Files to the Recycle Bin

The methods that we will examine for sending files and folders to the recycle bin are found in the Microsoft.VisualBasic.FileIO namespace, which is part of the Microsoft.VisualBasic assembly. To begin, add a reference to this assembly to your project. To make the code easier to read, you should also include the following using directive at the top of your code file:

using Microsoft.VisualBasic.FileIO;

With the reference and the using directive in place, we can recycle files using the DeleteFile method from the FileSystem class. This static method can be used to permanently delete files or send them to the Recycle Bin, depending upon the arguments that you specify. For the first example we will use an overloaded version of the method that accepts three parameters. The first is a string that contains the path to the file to be erased. The second parameter determines the level of interactivity with the user and the third indicates that we wish to use the Recycle Bin.

To send a file named "RecycleMe.txt" from the "c:\Recycle" folder to the Recycle Bin, use the following code:

FileSystem.DeleteFile(
    @"c:\Recycle\RecycleMe.txt", UIOption.AllDialogs, RecycleOption.SendToRecycleBin);

When the above is executed, a dialog box is displayed asking for confirmation of the deletion. If the user confirms the action, the file is sent to the Recycle Bin. If the user cancels the process, an exception of the type OperationCanceledException is thrown.

If you do not wish to display the confirmation dialog box when deleting files, you can hide it by changing the second parameter. Instead of using the AllDialogs value from the UIOption enumeration, use OnlyErrorDialogs. As the name suggests, this causes the DeleteFile method to display a dialog box only if an error occurs whilst attempting to delete the file.

FileSystem.DeleteFile(
    @"c:\Recycle\RecycleMe.txt", UIOption.OnlyErrorDialogs, RecycleOption.SendToRecycleBin);

Sending Folders to the Recycle Bin

The FileSystem class includes a second static method that allows you to send an entire folder, including all contained subfolders and files, to the Recycle Bin. DeleteDirectory accepts the same parameters as DeleteFile, except that the first argument provides the path to a folder rather than a file. For example, the sample below sends the entire "c:\Recycle" folder to the Recycle Bin.

FileSystem.DeleteDirectory(
    @"c:\Recycle", UIOption.AllDialogs, RecycleOption.SendToRecycleBin);

Cancellation Options

When using the UIOption.AllDialogs setting, the user is able to cancel the deletion of files or folders using a confirmation dialog box. As we have seen, if the user does cancel an exception is thrown. However, sometimes it will be valid for the user to cancel without your code taking further action. To allow for these situations, you can add a fourth parameter to the method. This parameter accepts a value from the UICancelOption enumeration. To retain the exception, you can use the following syntax:

FileSystem.DeleteDirectory(
    @"c:\Recycle", UIOption.AllDialogs, RecycleOption.SendToRecycleBin,
    UICancelOption.ThrowException);

Alternatively, to ignore the result of the confirmation, use the DoNothing constant. The following sample does not throw an exception if the user halts the process.

FileSystem.DeleteDirectory(
    @"c:\Recycle", UIOption.AllDialogs, RecycleOption.SendToRecycleBin,
    UICancelOption.DoNothing);

Limitations

The DeleteFile and DeleteDirectory methods are only supported for use in interactive environments, such as those in a console application or Windows Forms application. They are not supported for use in non-interactive scenarios such as Windows services.

This concludes the first part of the article. In the second part we will examine the process of sending items to the Recycle Bin using P/Invoke.

9 May 2009