none
How to invoke the Interface IFileOperation in VB.NET

    Question

  • I want to invoke the Interface IFileOperation using VB.NET in Windows 7, delete files or directories locate on different paths to Recycle Bin once, and show the Windows deleting UI, but do not know how to start the Interface IFileOperation, could anyone please show some codes, thank you very much.
    Wednesday, August 04, 2010 1:40 AM

Answers

  • There many ways to send file to recyclebin

    Using .net framework

    'Delete File:
    My.Computer.FileSystem.DeleteFile("THE FILE TO DELETE", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)

    'Delete Directory:
    My.Computer.FileSystem.DeleteDirectory("THE DIRECTORY TO DELETE", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)


    Using shell32
     Imports shell32  'You have to add reference to shell32 in system32 directory
      
        Private Const ssfBITBUCKET As Long = 10

        'OPTIONS
        '4 = Do not display a progress dialog box.
        '8 = Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
        '16 = Respond with "Yes to All" for any dialog box that is displayed.
        '64 = Preserve undo information, if possible.
        '128 = Perform the operation on files only if a wildcard file name (*.*) is specified.
        '256 = Display a progress dialog box but do not show the file names.
        '512 = Do not confirm the creation of a new directory if the operation requires one to be created.
        '1024 = Do not display a user interface if an error occurs.
        '2048 = Version 4.71. Do not copy the security attributes of the file.
        '4096 = Only operate in the local directory. Don't operate recursively into subdirectories.
        '9182 = Version 5.0. Do not move connected files as a group. Only move the specified files.

        Sub DeleteFileDialog(ByVal FileName As String)
            Dim Sh As New Shell
            Dim RecycleBin As Shell32.Folder = Sh.NameSpace(ssfBITBUCKET)
            RecycleBin.MoveHere(FileName, 64 Or 16)    'Options, substitude any option you         'RecycleBin.MoveHere(FileName, 1024 Or 64 Or 16 Or 4)
        End Sub

    using SHFileOperation
    more information at http://www.pinvoke.net/default.aspx/shell32.shfileoperation
     Private 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 Boolean
            Dim hNameMappings As Integer
            Dim lpszProgressTitle As String
        End Structure

        Private Const FO_DELETE As Integer = &H3S
        Private Const FOF_ALLOWUNDO As Integer = &H40S
        Private Const FOF_NOCONFIRMATION As Integer = &H10S


        Public Function SendFileToRecycle(ByRef sPath As String) As Integer
            Dim shf As SHFILEOPSTRUCT
            If Not IO.File.Exists(sPath) Then
                MsgBox(sPath & " not found", MsgBoxStyle.Exclamation, "File not found")
                SendFileToRecycle = -1
                Exit Function
            End If
            With shf
                .wFunc = FO_DELETE
                .pFrom = sPath & vbNullChar
                .pTo = vbNullChar
                .fFlags = FOF_NOCONFIRMATION Or FOF_ALLOWUNDO
                .lpszProgressTitle = "Sending " & sPath & " to the Recycle Bin"
            End With
            Try
                SHFileOperation(shf)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            SendFileToRecycle = 0
        End Function

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Wednesday, August 04, 2010 1:14 PM

All replies

  • If you first can give us an idea why you use it and not the normal Net solutions for that.

    As far as I can see is the IFileOperation not a Net interface and almost completely covered by Net classes.

     


    Success
    Cor
    Wednesday, August 04, 2010 6:39 AM
  • Want to delete some files or directories locate different disk paths once with standard Windows progress UI. Thank you.
    Wednesday, August 04, 2010 8:34 AM
  • There many ways to send file to recyclebin

    Using .net framework

    'Delete File:
    My.Computer.FileSystem.DeleteFile("THE FILE TO DELETE", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)

    'Delete Directory:
    My.Computer.FileSystem.DeleteDirectory("THE DIRECTORY TO DELETE", FileIO.UIOption.AllDialogs, FileIO.RecycleOption.SendToRecycleBin)


    Using shell32
     Imports shell32  'You have to add reference to shell32 in system32 directory
      
        Private Const ssfBITBUCKET As Long = 10

        'OPTIONS
        '4 = Do not display a progress dialog box.
        '8 = Give the file being operated on a new name in a move, copy, or rename operation if a file with the target name already exists.
        '16 = Respond with "Yes to All" for any dialog box that is displayed.
        '64 = Preserve undo information, if possible.
        '128 = Perform the operation on files only if a wildcard file name (*.*) is specified.
        '256 = Display a progress dialog box but do not show the file names.
        '512 = Do not confirm the creation of a new directory if the operation requires one to be created.
        '1024 = Do not display a user interface if an error occurs.
        '2048 = Version 4.71. Do not copy the security attributes of the file.
        '4096 = Only operate in the local directory. Don't operate recursively into subdirectories.
        '9182 = Version 5.0. Do not move connected files as a group. Only move the specified files.

        Sub DeleteFileDialog(ByVal FileName As String)
            Dim Sh As New Shell
            Dim RecycleBin As Shell32.Folder = Sh.NameSpace(ssfBITBUCKET)
            RecycleBin.MoveHere(FileName, 64 Or 16)    'Options, substitude any option you         'RecycleBin.MoveHere(FileName, 1024 Or 64 Or 16 Or 4)
        End Sub

    using SHFileOperation
    more information at http://www.pinvoke.net/default.aspx/shell32.shfileoperation
     Private 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 Boolean
            Dim hNameMappings As Integer
            Dim lpszProgressTitle As String
        End Structure

        Private Const FO_DELETE As Integer = &H3S
        Private Const FOF_ALLOWUNDO As Integer = &H40S
        Private Const FOF_NOCONFIRMATION As Integer = &H10S


        Public Function SendFileToRecycle(ByRef sPath As String) As Integer
            Dim shf As SHFILEOPSTRUCT
            If Not IO.File.Exists(sPath) Then
                MsgBox(sPath & " not found", MsgBoxStyle.Exclamation, "File not found")
                SendFileToRecycle = -1
                Exit Function
            End If
            With shf
                .wFunc = FO_DELETE
                .pFrom = sPath & vbNullChar
                .pTo = vbNullChar
                .fFlags = FOF_NOCONFIRMATION Or FOF_ALLOWUNDO
                .lpszProgressTitle = "Sending " & sPath & " to the Recycle Bin"
            End With
            Try
                SHFileOperation(shf)
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
            SendFileToRecycle = 0
        End Function

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Wednesday, August 04, 2010 1:14 PM
  • Thank you, but i want to delete together lots of files or directories located on different paths but no a file or directory, and system will show a standard Windows progress UI for all these deleted files, it seem that you select some files or directories located on different paths in Windows Explorer, then click delete. Please view the article below
    http://msdn.microsoft.com/en-us/magazine/cc163304.aspx
    In this article code, i do not know how to delete  together lots of files or directories.

    Thursday, August 05, 2010 7:31 AM
  • Like you can see it is Com (the previous technology) which is used (wrapped) in Net.

    If you want to go that way, then take one of the converters which Kaymaf shows of take this one.

    http://tangiblesoftwaresolutions.com/Product_Details/Instant_VB.html

    But be awared, you have less chance that it will work in future than with true Net solutions.

    Helping with that makes not much sense for most of us, because we have solutions enough to do this and the work will probably the same.

    It needs some work, don't expect somebody else does that for you for free.

    You are helped here, but you have to do the work yourself.

    (Or hire somebody to do the work for you).

    Especially if you have special desires. 


    Success
    Cor
    Thursday, August 05, 2010 8:27 AM
  • Thank you, but i want to delete together lots of files or directories located on different paths but no a file or directory, and system will show a standard Windows progress UI for all these deleted files, it seem that you select some files or directories located on different paths in Windows Explorer, then click delete. Please view the article below
    http://msdn.microsoft.com/en-us/magazine/cc163304.aspx
    In this article code, i do not know how to delete  together lots of files or directories.


    if you have problem with the code in that article, send the author an email and explain your scenerio about what you want to do. He already provided the email address at the bottom of the article.

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Thursday, August 05, 2010 10:45 AM
  • Hello kaymaf,
    How to delete lots of files and folders located on different paths using SHFileOperation, and show a standard Windows progress UI for all these deleted files and folders, thank you.

    Saturday, August 07, 2010 11:12 PM
  • Hello kaymaf,
    How to delete lots of files and folders located on different paths using SHFileOperation, and show a standard Windows progress UI for all these deleted files and folders, thank you.

    You can put all the paths in array of string or container like listbox or combobox and use loop to get each at a time.

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Sunday, August 08, 2010 2:19 PM
  • That will show muliti-Windows progress UI but no ONE? thank you.
    Sunday, August 08, 2010 10:50 PM
  • That will show muliti-Windows progress UI but no ONE? thank you.


    Well, you found article that did what you want, why can't you download the project, open it and compile FileOperation project to FileOperation.dll and you can reference it in your VB.NET so that you won't have to worry about convert the code from C# to VB. I just compiled it to dll but i cant test now because im on XP, that is for Vista Machine and it might work on win7.

    kaymaf


    CODE CONVERTER SITE

    http://www.carlosag.net/Tools/CodeTranslator/.

    http://www.developerfusion.com/tools/convert/csharp-to-vb/.

    Monday, August 09, 2010 12:37 AM
  • Thank you, we have done that, compiled FileOperation project to FileOperation.dll and referenced it in VB.NET in Windows 7.
    But it only deleted a file using the DeleteItem function one time, do not know how to invoke the DeleteItems to delete some files one time, and i have contacted the author, but no result.
    Monday, August 09, 2010 5:15 AM