c# - 如何从回收站获取已安装卷/磁盘的文件列表?

标签 c# filesystems com-interop vhd recycle-bin

通常人们使用Shell32.dll获取回收站内的文件列表。

private static IEnumerable<string> GetRecycleBinFilenames()
{
    const int ssfBitbucket = 10;
    Type t = Type.GetTypeFromProgID("Shell.Application");
    dynamic shell = Activator.CreateInstance(t);
    Folder recycleBin = shell.NameSpace(ssfBitbucket);

    foreach (FolderItem2 recfile in recycleBin.Items())
    {
        yield return recfile.Path;
    }

    Marshal.FinalReleaseComObject(shell);
}

我正在装载 VHDX 文件,并希望从已装载的外部磁盘/卷上的回收站获取文件列表。我怎样才能做到这一点?

最佳答案

@RaymondChen suggested您可以根据 Path property 进行过滤,其中包含已删除项目的当前路径...

foreach (FolderItem2 recfile in recycleBin.Items())
{
    if (Path.GetPathRoot(recfile.Path) == "X:\\")
        yield return recfile.Path;
}

您还可以检索删除项目的目录路径并以相同的方式进行过滤...

const int BitBucketItem_OriginalParentPath = 1;

foreach (FolderItem2 recfile in recycleBin.Items())
{
    string originalParentPath = recycleBin.GetDetailsOf(recfile, BitBucketItem_OriginalParentPath);

    if (Path.GetPathRoot(originalParentPath) == "X:\\")
        yield return recfile.Path;
}

...但是,如果通过结点/符号链接(symbolic link)删除该项目,则 originalParentPath 将包含结点/符号链接(symbolic link)的驱动器,该驱动器不一定是存储已删除项目的驱动器。

关于c# - 如何从回收站获取已安装卷/磁盘的文件列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37169214/

相关文章:

c# - Windows 服务在测试时有效但不作为服务

c# - 使用非空值初始化 List<T>

linux - chroot 替代

java - 是否可以使用 Java 格式化内存棒、笔式驱动器或磁盘?

windows - 如果我违反了对 ProgID 的要求会怎样?

c# - WPF:在 ItemsControl 中定位项目并启用滚动

c# - 在大型 C# 应用程序上执行批量命名空间重命名的最佳方法是什么?

python - OS X 上 Python 中的看门狗库——未显示完整的事件路径

c# - 以编程方式启动和停止 IIS Express

c# - .NET COM 互操作方法签名