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

末鹿安然 提交于 2019-11-30 18:47:07

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/18071412/list-filenames-in-the-recyclebin-with-c-sharp-without-using-any-external-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!