Given a folder, how can I tell that it is a recycle bin? I've found an answer for C++ but not for C#.
My first idea was to check for FileAttributes.System (which would be an acceptable approximation in my case) but actually that flag is cleared on the recycle folder.
Crude solutions using hardcoded folder names are out of the question (we're in 2009 after all).
Most of the recycle bin related methods have been written in C++ as you mentioned. You could create a wrapper class in your application using the managed extensions to C++, then you will have to use DLLImport like this:
There are also articles out there that do this some other way with C#, most of them use PInvoke or rely on the folder having $Recycle in it's name. Following are a few links I've found for this subject
http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/05f1476f-a101-4766-847b-0bdf4f6ad397
http://www.codeproject.com/KB/shell/recyclebin.aspx
http://www.pinvoke.net/default.aspx/shell32.SHFileOperation
Microsoft's Windows API Code Pack contains this functionality.
To get the folder of the Recycle Bin, use
I've no idea what that string means, but it was included in the docs as the reference to the Recycle Bin.
Hope this helps :)
There's a little problem here. The Windows Recycle Bin is a virtual folder and does not actually exist. The files that you see are not actually in that folder, they are the representation of existing files on disk that have been renamed to a special name, which "removes" them from the visible file system, but not the physical one.
You can "proof" this for yourself by asking for the folder location using the win32 API. It will return
E_FAIL
for the Recycle Bin, but not for other folders (see SHGetKnownFolderPath on pinvoke.net (and on MSDN) for all constants you can use and the declarations needed for this code to run):The fake foldername "$Recycle.bin" is repeated for each drive. The hidden name is not stored in the registry and it is not accessible by the API as such. The earlier suggested KnownFolderHelper will not retrieve this information either (the same lib has a named method for getting the Recycle Bin, it also has a
GetPath
, it will turn up empty).But all is not lost. This fake non-existing "file name" or "folder name" contains a hidden file that looks something like "S-1-5-21-2703390745-3900912742-210389625-1000" (yours will be different). It's one of two "reliable" ways to find out whether a certain filename is actually a virtual directory of the recycle bin (the other way being: delete a file through
SHFileOperation
, explained here, and check whether it appears in the folder you have):Note: I don't know what the hidden folders are on other win32 versions, you'l have to experiment a bit. They all have the system and hidden flag set and look like a mangled GUID.
The API docs are not very clear about it, but if you need confirmation, this page explains that there really is no path that can be retrieved (the older CSIDL related page is much less clear on it).
Update: alternative approaches with
SHGetSpecialFolderPath
,SHGetSpecialFolderLocation
,ShellAPI.SHGetFolderLocation
andSHGetPathFromIDList
all fail with the same: either an empty result or an error. I tested all functions both for Recycle Bin and for AppData (to be sure I used the correct parameters).Only the documentation on
ShGetPathFromIDListEx
said it explicitly, quote: "Except for UNC printer names, if the location specified by the pidl parameter is not part of the file system, this function fails.".