Hello again.
I'm trying to send a file to the recycle bin using the SHFileOperation API :
I declared the API and the SHFILEOPSTRUCT structure like this :
I load all the files in a list box, like this :Code:<DllImport("shell32.dll", EntryPoint:="SHFileOperation", SetLastError:=True, CharSet:=CharSet.Auto)> _ Private Shared Function SHFileOperation(ByRef lpFileOp As SHFILEOPSTRUCT) As Integer End Function <StructLayout(LayoutKind.Sequential)> _ Private Structure SHFILEOPSTRUCT Public hWnd As Int32 Public wFunc As Int32 Public pFrom As String Public pTo As String Public fFlags As Int16 Public fAborted As Int32 Public hNameMaps As Int32 Public sProgress As String End Structure Private Const FO_DELETE As Short = &H3S Private Const FOF_ALLOWUNDO As Short = &H40S Private Const FOF_NOCONFIRMATION As Short = &H10S Private SelFileName As String
Then, I get the selected file like this :Code:Private Sub LoadFiles() Dim Filename As String For Each Filename In Directory.GetFiles(Application.StartupPath) ListBox1.Items.Add(Filename) Next End Sub Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load LoadFiles() End Sub
NOW, I'm trying this :Code:Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged SelFileName = ListBox1.SelectedItem.ToString End Sub
What happens is that I get a messagebox that the system cannot delete the file because the it cannot read from source file or disk.Code:Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim SHop As SHFILEOPSTRUCT With SHop .wFunc = FO_DELETE .pFrom = SelFileName .pTo = String.Empty .fFlags = FOF_NOCONFIRMATION Or FOF_ALLOWUNDO .sProgress = "Sending " & SelFileName & " to the Recycle Bin" End With Try SHFileOperation(SHop) Catch ex As Exception MessageBox.Show(ex.Message.ToString()) End Try End Sub
How can I send the file to the recycle bin using this API ?




Reply With Quote