注意:为了清楚起见,我不想像在其他问题中回答的那样清空回收站。。
使用来自堆栈溢出和超级用户的其他问题的答案,我发现在C#中获取一个人的回收站位置的方法是:
@"C:\$Recycle.Bin\" + System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString()
但是,当我运行以下代码时,我得到的文件与我的回收站中的文件不同:
string location = @"C:\$Recycle.Bin\" + System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString();
foreach (string file in Directory.GetFiles(location))
{
Console.Writeline(file);
} 我怎样才能正确地得到我的回收垃圾箱中的所有文件?我的需求是在最后一次使用这些文件时访问它们,然后可能的话恢复它们。
谢谢。
发布于 2017-02-20 01:07:50
因为回收站没有物理位置,它是一个虚拟文件夹,所以它不像最初想象的那么直接。要获取回收站中所有文件的列表并完成这项工作,您需要添加对files 32.dll的引用(位于%SystemRoot%\system32 32\files 32.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();
}参考文献:http://www.dreamincode.net/forums/topic/161500-play-with-recycle-bin-in-code/
要获得文件的LastModified属性,您可以在这里看到我的答案:https://stackoverflow.com/a/11660616/
还要注意,每个硬盘都有一个回收站,所以你必须检查所有的驱动器。
要恢复文件,这里是C#:https://stackoverflow.com/a/6025331/495455中的解决方案
https://stackoverflow.com/questions/42334738
复制相似问题