list filenames in the recyclebin with c# without using any external files
Asked Answered
N

1

5

I would like to have a function which retrieves me the filenames in the recycle bin (on win 7) with usage of c# code. The framework seems nothing to contain to achieve this.

Directory.Getfiles() wouldn't work on it, would it?

I found myself a code with using "windows shell32 automation" but this requires to supply interop.shell32.dll which must be redistributed with my application. Since my application should work standalone (as long Net Framework 2 exists on users computer), this solution isn't ideal. This solution seems also to be unreliable to me, since it uses COM Automation and some references could fail or calling Namespace(10) could fail. I'm even not sure if the Namespace(10) is always the right folder for the recycle bin. This code was all what I found for C# on this matter.

I found a Delphi and another implementation on code project, but if I copy that code in my C# project it is practically all red underlined and it won't compile. I'm not sure how to correct this code to make it working with in C#. On the pinvoke.net I found no code for the windows function which I would have to use (SHQueryRecycleBin), but maybe I must use even more functions to get the filelist from the recycle bin.

Has anyone seen a code or any ideas?

Nourishment answered 6/8, 2013 at 3:34 Comment(3)
Well, if you copied in some Delphi code, that probably wouldn't compile with C#...Sideboard
That's correct, C++ code will not compile with a C# compiler. You can only compile C# with a C# compiler; hence the name ("C# compiler")...Sideboard
@Nourishment A lot is compatible with Visual Studio, that does not mean that each language compatible with Visual Studio will use the C# compiler. As feralin said; You can only compile C# with the C# compiler.Divided
O
7

If you add a reference C:\Windows\System32\Shell32.dll to in your application you can easily access the filenames of the files in the Recyle Bin

Example:

    using Shell32;

    public IEnumerable<string> GetRecycleBinFilenames()
    {
        Shell shell = new Shell();
        Folder recycleBin = shell.NameSpace(10);

        foreach (FolderItem2 recfile in recycleBin.Items())
        {
            // Filename
            yield return recfile.Name;

            // full recyclepath
            // yield return recfile.Path;
        }

        Marshal.FinalReleaseComObject(shell);
    }

If the extra file created bothers you you can embed it in the executable

enter image description here

Ofori answered 6/8, 2013 at 4:1 Comment(3)
Set the properties of Shell32 to Embed Iterop Types if you dont want the extra file, this will work on any Win7 machineOfori
Added picture on where to set, It should be backward compatable on older windows versions but I can't test right nowOfori
If you want to use an enum for readability, you can use: shell.NameSpace(ShellSpecialFolderConstants.ssfBITBUCKET)Idiopathy

© 2022 - 2024 — McMap. All rights reserved.