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

时间:2016-05-11 17:11:55

标签: 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文件,并希望从已安装的外部磁盘/卷上的回收站中获取文件列表。我怎么能这样做?

1 个答案:

答案 0 :(得分:0)

作为@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;
}

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

相关问题