使用c#在recyclebin中列出文件名,而不使用任何外部文件 [英] list filenames in the recyclebin with c# without using any external files

查看:202
本文介绍了使用c#在recyclebin中列出文件名,而不使用任何外部文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有一个函数,它使用c#代码在回收站(在win 7上)中检索我的文件名。框架似乎没有包含实现这一点。



Directory.Getfiles()不能工作,是吗?

<我发现自己的代码使用Windows Shell32自动化,但这需要提供interop.shell32.dll,必须与我的应用程序重新分配。由于我的应用程序应该独立工作(只要用户计算机上存在Net Framework 2),该解决方案并不理想。
这个解决方案对我来说似乎也是不可靠的,因为它使用COM Automation,有些引用可能会失败,或者调用Namespace(10)可能会失败。我甚至不确定名称空间(10)是否始终是回收站的正确文件夹。
这段代码就是我在C#上找到的所有东西。



我在代码项目中找到了一个Delphi和另一个实现,但是如果我复制代码我的C#项目几乎所有的红色下划线,它不会编译。我不知道如何纠正这个代码,使其在C#中的工作。在pinvoke.net上,我没有找到windows函数的代码(SHQueryRecycleBin),但也许我必须使用更多的函数才能从回收站获取文件列表。

有没有人看过一个代码或任何想法?

解决方案

如果添加引用 C:\Windows\System32\Shell32.dll 在您的应用程序中,您可以轻松访问Recyle Bin中的文件的文件名



<例如:

 使用Shell32; 

public IEnumerable< string> GetRecycleBinFilenames()
{
Shell shell = new Shell();
文件夹recycleBin = shell.NameSpace(10);
$ b foreach(RecycleBin.Items()中的FolderItem2 recfile)
{
//文件名
yield return recfile.Name;

//完整的循环路径
//产量返回recfile.Path;
}

Marshal.FinalReleaseComObject(shell);



$ b $ p
$ b

如果创建的额外文件困扰您,您可以将其嵌入到可执行文件中 p>


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?

解决方案

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

这篇关于使用c#在recyclebin中列出文件名,而不使用任何外部文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆