
.NET 1.1+Recycling Files and Folders Part 2 (2)
The second part of this two-part article revisits sending files and folders to the Windows Recycle Bin. In the first part we used the VisualBasic namespace to perform this task. In the second part we will make use of the Windows API through P/Invoke.
Sending Files to the Recycle Bin
As with the previous part of the article we will begin with the simplest operation. This sends a file to the Recycle Bin and provides an interactive process. When the command is issued, the user is presented with a dialog box that asks them to confirm that they wish to delete the file. If they answer, "Yes", the file is moved to the Recycle Bin. If they click the "No" button, the operation is cancelled.
To perform the recycling operation we must create a SHFILEOPSTRUCT structure containing three elements. The wFunc field must contain the delete command constant, "FO_DELETE". The pFrom value should be set to the name of the file to be deleted, including the full path, and should be double null-terminated. Finally, the fFlags field is set to FOF_ALLOWUNDO to indicate that the Recycle Bin is to be used.
To send a file named "RecycleMe.txt" from the "c:\Recycle" folder to the Recycle Bin, use the following code. Note the addition of the "\0\0" string to the file name to specify the double null string-termination.
SHFILEOPSTRUCT operation = new SHFILEOPSTRUCT();
operation.wFunc = FO_DELETE;
operation.pFrom = @"c:\Recycle\RecycleMe.txt" + "\0\0";
operation.fFlags = FOF_ALLOWUNDO;
int result = SHFileOperation(ref operation);
If you have already confirmed the deletion with the user, or if you do not require confirmation, you may not wish to show the confirmation dialog box. To remove this dialog box, include the FOF_NOCONFIRMATION flag.
SHFILEOPSTRUCT operation = new SHFILEOPSTRUCT();
operation.wFunc = FO_DELETE;
operation.pFrom = @"c:\Recycle\RecycleMe.txt" + "\0\0";
operation.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
int result = SHFileOperation(ref operation);
You should have noticed that the previous example did not request confirmation but did show a progress dialog box. This is more noticeable when you are deleting a large number of files or folders. It is likely that you will not wish to show this progress window. To hide it, add the FOF_SILENT flag.
SHFILEOPSTRUCT operation = new SHFILEOPSTRUCT();
operation.wFunc = FO_DELETE;
operation.pFrom = @"c:\Recycle\RecycleMe.txt" + "\0\0";
operation.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_SILENT;
int result = SHFileOperation(ref operation);
The last flag that we will consider is important when dealing with large files. Some files are too large to be placed into the Recycle Bin. When these are removed they are destroyed instead. To allow the user the opportunity to cancel the operation when such files will be erased, include the FOF_WANTNUKEWARNING flag. This provides a partial overriding of the FOF_NOCONFIRMATION flag where supported.
SHFILEOPSTRUCT operation = new SHFILEOPSTRUCT();
operation.wFunc = FO_DELETE;
operation.pFrom = @"c:\Recycle\RecycleMe.txt" + "\0\0";
operation.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_WANTNUKEWARNING;
int result = SHFileOperation(ref operation);
Recycling Multiple Files Using Wildcards
The SHFileOperation function allows you to send multiple files to the Recycle Bin in a single call. This is achieved by using the standard wildcard characters for file operations. An asterisk (*) indicates that any series of characters will be matched. A question mark (?) matches any single character. The wildcards can be combined with text to define a pattern to match within the filename portion of the pFrom field. You may not use wildcards in the path element.
For example, to recycle all files from the "c:\Recycle" folder, execute the following command. As before, you can change the flags to modify the behaviour:
SHFILEOPSTRUCT operation = new SHFILEOPSTRUCT();
operation.wFunc = FO_DELETE;
operation.pFrom = @"c:\Recycle\*.*" + "\0\0";
operation.fFlags = FOF_ALLOWUNDO;
int result = SHFileOperation(ref operation);
Sending Folders to the Recycle Bin
Finally, SHFileOperation can be used to send entire folders to the Recycle Bin. This deletes the folder and all of its contents with a single call to the function. To specify that you wish to remove a folder, simply provide the folder name in the pFrom field of the SHFILEOPSTRUCT structure. The folder name should not include a trailing backslash but, as with filenames, it should be double null-terminated.
SHFILEOPSTRUCT operation = new SHFILEOPSTRUCT();
operation.wFunc = FO_DELETE;
operation.pFrom = @"c:\Recycle" + "\0\0";
operation.fFlags = FOF_ALLOWUNDO;
int result = SHFileOperation(ref operation);
21 May 2009