在VB程序中通过回收站删除文件
作者:土人
在标准工程中添加一个公共对话框和两个按钮即可尝试本例:
Option Explicit
注释:删除文件的API
Private Declare Function SHFileOperation Lib "shell32.dll" Alias "SHFileOperationA" (lpFileOp As ToBin) As Long
注释:清空回收站的API
Private Declare Function SHEmptyRecycleBin Lib "shell32.dll" Alias "SHEmptyRecycleBinA" (ByVal hwnd As Long, ByVal pszRootPath As String, ByVal dwFlags As Long) As Long
Private Type ToBin
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
Const FO_DELETE = &H3
Const FOF_ALLOWUNDO = &H40
Const SHERB_NORMAL = &H0
注释:将文件移至回收站
Private Sub Command1_Click()
Dim Go As ToBin
Dim strFile As String
With CommonDialog1
.Filter = "(*.bak)|*.bak"
.DialogTitle = "删除文件"
.ShowOpen
strFile = .FileName
End With
With Go
.wFunc = FO_DELETE
.pFrom = strFile
.fFlags = FOF_ALLOWUNDO
End With
SHFileOperation Go
End Sub
注释:清空回收站
Private Sub Command2_Click()
Dim RetVal As Long
RetVal = SHEmptyRecycleBin(0&, vbNullString, SHERB_NORMAL)
End Sub