Reputation: 1595
NOTE: To be clear, I do not want to empty the recycle bin as answered in other questions.
Using answers from other questions on both Stack Overflow and Super User I have discovered the way to get the location of one's recycle bin in C# is:
@"C:\$Recycle.Bin\" + System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString()
However, when I run the following code I am getting different files to what is actually in my recycle bin:
string location = @"C:\$Recycle.Bin\" + System.Security.Principal.WindowsIdentity.GetCurrent().User.ToString();
foreach (string file in Directory.GetFiles(location))
{
Console.Writeline(file);
}
How can I correctly get all the files in my recycling bin? My needs are to access when the files were last used and to possibly then restore them.
Thanks.
Upvotes: 2
Views: 1387
Reputation: 65554
It's not as straight forward as initially thought because the Recycle Bin doesn't have a physical location, it's a virtual folder. To get a list of all files in the Recycle Bin and make this work you need to add a reference to shell32.dll (located in %SystemRoot%\system32\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();
}
REF: http://www.dreamincode.net/forums/topic/161500-play-with-recycle-bin-in-code/
To get the LastModified Property of the files you can see my answer here: https://stackoverflow.com/a/11660616/
Also note there's a Recycle Bin for each HDD so you will have to check all drives.
To restore files here is the solution in C#: https://stackoverflow.com/a/6025331/495455
Upvotes: 2