Ken, I replied twice but the note ends up under Herfried's note. Here's a
copy (revised).
I did try this of course, but I'm surprised that no consideration
seems to be given to the fact that pFrom is supposed to be a string that is
terminated with an extra zero? As I remember, it is a list of filenames
separated by semicolons. Maybe this takes the chance that the byte after the
file name does not happen to be a semicolon??
Thanks very much
Also, when I tried it I get "Cannot delete file: Cannot read from source
file or disk." error.
I've included the code below:
Imports System.Runtime.InteropServices
Public Class Form1
Inherits System.Windows.Forms.Form
Public Const FO_DELETE As Integer = &H3
Public Const FOF_ALLOWUNDO As Integer = &H40
Public Const FOF_SIMPLEPROGRESS As Integer = &H100
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents OpenFileDialog1 As System.Windows.Forms.OpenFileDialog
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.OpenFileDialog1 = New System.Windows.Forms.OpenFileDialog
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(128, 160)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(104, 40)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
End Sub
#End Region
Structure SHFILEOPSTRUCT
Dim hwnd As Integer
Dim wFunc As Integer
Dim pFrom As String
Dim pTo As String
Dim fFlags As Short
Dim fAnyOperationsAborted As Integer
Dim hNameMappings As Integer
Dim lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS
End Structure
<DllImport("shell32.dll", entrypoint:="SHFileOperationA", _
setlasterror:=True, CharSet:=CharSet.Auto, exactspelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT) As
Integer
End Function
Private Sub Button1_Click1(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim sh As New SHFILEOPSTRUCT
With sh
..wFunc = FO_DELETE
..pFrom = OpenFileDialog1.FileName
..fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS
End With
If SHFileOperation(sh) <> 0 Then
MessageBox.Show("Error deleting file")
End If
End If
End Sub
End Class
"Ken Tucker [MVP]" <vb***@bellsouth.net> wrote in message
news:uw**************@TK2MSFTNGP11.phx.gbl...
Hi,
Here is the structure, dll declare, and sample code. It will
allow you to select a file to delete with an open file dialog and send it to the
recycle bin.
Structure SHFILEOPSTRUCT
Dim hwnd As Integer
Dim wFunc As Integer
Dim pFrom As String
Dim pTo As String
Dim fFlags As Short
Dim fAnyOperationsAborted As Integer
Dim hNameMappings As Integer
Dim lpszProgressTitle As String ' only used if FOF_SIMPLEPROGRESS
End Structure
<DllImport("shell32.dll", entrypoint:="SHFileOperationA", _
setlasterror:=True, CharSet:=CharSet.Auto, exactspelling:=True, _
CallingConvention:=CallingConvention.StdCall)> _
Public Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT)
As Integer
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
Dim sh As New SHFILEOPSTRUCT
With sh
.wFunc = FO_DELETE
.pFrom = OpenFileDialog1.FileName
.fFlags = FOF_ALLOWUNDO Or FOF_SIMPLEPROGRESS
End With
If SHFileOperation(sh) <> 0 Then
MessageBox.Show("Error deleting file")
End If
End If
End Sub
Ken