You’ll often need to equip your VB applications with a way for users to delete files they no longer require. Unfortunately, the easiest way to handle this—using the Kill function—is a one-way ticket. If users inadvertently delete a file, or if you need a deleted file for debugging purposes, there’s no way to get it back.
Kill is convenient for developers because it doesn’t require an API call. All you have to do is pass a pathname for the file or files as a string expression to the function—for example, Kill (App.Path & “\1.txt”)—and the files are deleted. But if you want to give your users a safety net and make sure that their files are recoverable, you can take another approach. Instead of using the Kill function, have your apps send deleted files to the Recycle Bin. Here’s a look at how it works.
A trip to the Recycle Bin
The key to this technique is the SHFileOperation function, which is exported by Shell32.dll. SHFileOperation is supported under Windows 95/98/Me/NT4.0/2000 and allows copying, moving, renaming, and deleting objects in the file system. You can also use it to delete files by sending them to the Recycle Bin so that users can restore them if necessary.
The API
By calling SHFileOperation, you can leverage the existing dialogs for moving files and providing user feedback about the status of the process. To use the SHFileOperation function in a Visual Basic program, you must include the following Declare statement:
Public Declare Function SHFileOperation Lib _
“shell32.dll” Alias “SHFileOperationA” (lpFileOp _
As SHFILEOPSTRUCT) As Long
The SHFileOperation function returns 0 when the operation is successful and any nonzero value when an error occurs.
The function requires one argument, a pointer to a SHFILEOPSTRUCT structure. This structure contains information the SHFileOperation function needs to perform the requested file operation.
For Visual Basic, the format of the SHFILEOPSTRUCT structure looks like this:
Public Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As Long
End Type
The elements of the structure
Table A describes each of the elements of the SHFILEOPSTRUCT structure. Sending files to the Recycle Bin requires setting two options: FO_DELETE and FO_ALLOWUNDO.
Table A
|
Code demo
Let’s create a simple VB project to send a file to the Recycle Bin. For this sample function to work as designed, you’ll need to deselect the Recycle Bin option to remove files immediately when deleted, as shown in Figure A.
Figure A |
Recycle Bin Properties screen |
- Fire up VB and start a new project.
- Create a module and add the following code to it:
Option Explicit
Public Type SHFILEOPSTRUCT
hwnd As Long
wFunc As Long
pFrom As String
pTo As String
fFlags As Integer
fAnyOperationsAborted As Long
hNameMappings As Long
lpszProgressTitle As Long
End Type
Public Declare Function SHFileOperation Lib _
“shell32.dll” Alias “SHFileOperationA” (lpFileOp _
As SHFILEOPSTRUCT) As Long
Public Const FO_DELETE = &H3
Public Const FOF_ALLOWUNDO = &H40
- Add a command button to your form and call it cmdSendToRecycleBin.
Your screen should now look like Figure B.
Figure B |
Sample form |
- Add the following code to the cmdSendToRecycleBin Click event:
Dim SHop As SHFILEOPSTRUCT
Dim strFile As String
strFile = App.Path & “\hello.txt”
With SHop
.wFunc = FO_DELETE
.pFrom = strFile
.fFlags = FOF_ALLOWUNDO
End With
SHFileOperation SHop
- Modify the filename and location in strFile = App.Path & “\hello.txt” to reflect the file you want to send to the Recycle Bin. If the file is located in the same directory as the VB project, you can leave App.Path and just change the name of the file.
- Press [Ctrl][F5] to run the project.
- Click Send To Recycle Bin, and your screen should look like Figure C. Select Yes, and the file will be moved to the Recycle Bin.
Figure C |
Sample output |
More fun with VB and APIs
In this sample, we used the SHFileOperation API to send a specified file to the Recycle Bin, allowing users to restore files if necessary. Next time, we will look at some other API functions that come in handy for application development—functions that will enable users to log off, restart, or shut down their computers.