Microsoft KB Archive/154005

= How To Delete a File into the Win95/98/Me Recycle Bin =

Article ID: 154005

Article Last Modified on 7/1/2004

-

APPLIES TO


 * Microsoft Visual Basic 5.0 Learning Edition
 * Microsoft Visual Basic 6.0 Learning Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition
 * Microsoft Visual Basic 4.0 Standard Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 Professional Edition
 * Microsoft Visual Basic 4.0 16-bit Enterprise Edition
 * Microsoft Visual Basic 4.0 32-Bit Enterprise Edition

-



This article was previously published under Q154005



SUMMARY
The Visual Basic function called KILL enables a user to delete a file from disk. Under Windows 95, Windows 98, Windows Me, Windows NT 4.0, and Windows 2000, files can be deleted into the Recycle Bin, giving a user the ability to reclaim deleted items. The KILL function does not offer this functionality to the Visual Basic Programmer. This article describes how to use the SHFileOperation Win32 API to enable a user to delete a file and place it in the Recycle Bin. Below is a code sample showing how to use the API in Visual Basic.

The Recycle Bin option to remove files immediately when deleted should be turned off in order for this sample to function as designed.



MORE INFORMATION
 Start a new project in Visual Basic. Form1 is created by default. Place a Command button on Form1.  Add the following code to the Form1 code window: Option Explicit

Private Type SHFILEOPSTRUCT hwnd As Long wFunc As Long pFrom As String pTo As String fFlags As Integer fAnyOperationsAborted As Boolean hNameMappings As Long lpszProgressTitle As String End Type

Private Declare Function SHFileOperation Lib "shell32.dll" Alias _ "SHFileOperationA" (lpFileOp As SHFILEOPSTRUCT) As Long Private Declare Function GetTempFileName Lib "kernel32" _ Alias "GetTempFileNameA" (ByVal lpszPath As String, _       ByVal lpPrefixString As String, ByVal wUnique As Long, _        ByVal lpTempFileName As String) As Long

Private Const FO_DELETE = &H3 Private Const FOF_ALLOWUNDO = &H40

Sub Command1_Click Dim FileOperation As SHFILEOPSTRUCT Dim lReturn As Long Dim sTempFilename As String * 100 Dim sSendMeToTheBin As String lReturn = GetTempFileName("c:\", "VB_", 0, sTempFilename) sSendMeToTheBin = Left(sTempFilename, InStr(sTempFilename, _ Chr$(0))) With FileOperation .wFunc = FO_DELETE .pFrom = sSendMeToTheBin .pTO = vbNullChar  ' Not used .fFlags = FOF_ALLOWUNDO End With lReturn = SHFileOperation(FileOperation) MsgBox "View your Recycle Bin for files beginning with VB_" End Sub

 Press the F5 key to run the project, and click the Command button. You will be prompted to confirm the deletion of the file. In your Recycle Bin, there will be a file with the prefix "VB_" that is the file created by the GetTempFileName Win32 API call.

