Results 1 to 5 of 5

Thread: [RESOLVED] [2005] SHFileOperation recycle Bin

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Resolved [RESOLVED] [2005] SHFileOperation recycle Bin

    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 :
    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
    I load all the files in a list box, 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
    Then, I get the selected file like this :
    Code:
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    
            SelFileName = ListBox1.SelectedItem.ToString
    
        End Sub
    NOW, I'm trying this :
    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
    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.

    How can I send the file to the recycle bin using this API ?

  2. #2
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [2005] SHFileOperation recycle Bin

    Why use SHFileOPeration API when this can be done using a single line of code in 2005.
    vb Code:
    1. My.Computer.FileSystem.DeleteFile("C:\myFile.dat", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin, FileIO.UICancelOption.ThrowException)
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [2005] SHFileOperation recycle Bin

    Thanx for that! That is quite cool! That works!
    But for interest sake, how can I use the API properly, so that I can know both ? You see I learnt about this API in VB 6, so I'm trying to improve on that application.

  4. #4
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: [2005] SHFileOperation recycle Bin

    There is no need to learn about that API. This is the beauty of .NET 2.0, you don't have to worry about what is happening behind the scenes.

    Now for interest sake, the reason why your API is giving an error may be because you have a blank space in the path where the file is present. I have seen that giving a problem sometimes.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Posts
    384

    Re: [2005] SHFileOperation recycle Bin

    OK, thanx for the advice

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width