Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用c#控制台应用程序获取回收站中的文件列表_C#_.net_Com_Recycle Bin_Shell32 - Fatal编程技术网

如何使用c#控制台应用程序获取回收站中的文件列表

如何使用c#控制台应用程序获取回收站中的文件列表,c#,.net,com,recycle-bin,shell32,C#,.net,Com,Recycle Bin,Shell32,我正在做作业,通过使用shell32.dll获取回收站的文件计数。但是,我正在努力显示回收站中的文件列表,当我尝试使用shell时,System.InvalidCastException错误不断出现 我浏览了很多解决堆栈溢出的方法,其中大多数都使用shell32.dll来获取回收站的文件列表。我尝试的最新代码如下: public static void Main(string[] args) { Shell shell = new Shell(); Folder folder = she

我正在做作业,通过使用shell32.dll获取回收站的文件计数。但是,我正在努力显示回收站中的文件列表,当我尝试使用shell时,System.InvalidCastException错误不断出现

我浏览了很多解决堆栈溢出的方法,其中大多数都使用shell32.dll来获取回收站的文件列表。我尝试的最新代码如下:

public static void Main(string[] args)
{
  Shell shell = new Shell();
  Folder folder = shell.NameSpace(0x000a);

  foreach (FolderItem2 item in folder.Items())
    Console.WriteLine("FileName:{0}", item.Name);

  Marshal.FinalReleaseComObject(shell);
  Console.ReadLine();
}

此错误很可能是由于缺少方法上的
STAThread
。下面的示例是一个旧的测试,它实际上执行与您尝试执行的相同的操作。如果错误是在获取实际名称时,那么我注意到您的名称与我以前的做法不同。我请求该文件夹提供有关其文件的详细信息

using Shell32;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication1101
{
    class Program
    {
        [STAThread()]
        static void Main(string[] args)
        {
            // create shell
            var shell = new Shell();

            // get recycler folder
            var recyclerFolder = shell.NameSpace(10);

            // for each files
            for (int i = 0; i < recyclerFolder.Items().Count; i++)
            {
                // get the folder item
                var folderItems = recyclerFolder.Items().Item(i);

                // get file name
                var filename = recyclerFolder.GetDetailsOf(folderItems, 0);

                // write file path to console
                Console.WriteLine(filename);
            }
        }
    }
}
使用Shell32;
使用制度;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
命名空间控制台应用程序1101
{
班级计划
{
[STAThread()]
静态void Main(字符串[]参数)
{
//创建shell
var shell=新shell();
//获取回收文件夹
var recyclerFolder=shell.NameSpace(10);
//对于每个文件
对于(int i=0;i

GetDetailsOf
上,如果您需要有关该文件的任何其他信息,请告诉我们确切的错误以及错误发生在哪一行。并告诉我们您正在测试哪个版本的windows,以及哪个版本的.NET Framework。看起来至少在Windows7上是有效的,和你的略有不同。你也试过了吗?Windows版本的可能副本很重要,你需要在Main()方法中添加[StatThread]属性,以增加它在每个版本上正常工作的几率。它正在工作!!!谢谢你的帮助!希望我能完成作业。