c# - 如何使用 c# 控制台应用程序获取回收站中的文件列表
问题描述
我正在做作业以使用 shell32.dll 获取回收站的文件数。但是,我正在努力显示回收站中的文件列表,并在System.InvalidCastException
尝试使用 shell 时不断出错。
我在 Stack Overflow 上浏览过不少解决方案,大部分都是使用 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
on 方法。以下示例是一个旧测试,它实际上与您尝试做的事情相同。如果错误在于获取实际名称,那么我注意到您的名称与我过去的做法不同。我要求该文件夹向我提供有关其文件的具体详细信息。
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);
}
}
}
}
如果您需要有关GetDetailsOf
文件的任何其他信息,这里是帮助
推荐阅读
- java - Finding the type of variable, knowing it's a primitive datatype
- position - 如何在 gdscript 中获取不同对象的位置?
- node-modules - 你如何打包一个 tree-sitter 语法以供消费?
- c++ - Binding free functions as class member functions
- r - 如何使用 sjPlot 在 R Shiny 中报告 html 表?
- javascript - Dynamically Select An HTML option based previous select option
- c# - 添加到并发队列时阻止任务启动
- modelica - 如何使用 Dymola 添加单位换算
- javascript - React JS memory leak with periodic calculations using setInterval
- image - 当您在 Flutter 中以字节形式接收图像时,如何读取/写入具有原始扩展名的图像文件?