I'm looking for a way to put a folder (with subfolders) into a Recycle Bin with these conditions:
It must be done silently -- without any Windows UI.
The folder must never be permanently deleted. If it can't be put into Recycle Bin, I'd expect the API to fail.
Get a callback routine for the process like CopyFileEx does.
So far I was able to come up with this:
SHFILEOPSTRUCT sfo = {0};
sfo.wFunc = FO_DELETE;
sfo.pFrom = L"K:\\test del from USB\0"; //Folder on a USB stick
sfo.fFlags = FOF_ALLOWUNDO |
FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_NOCONFIRMMKDIR |
FOF_WANTNUKEWARNING;
int res = SHFileOperation(&sfo);
BOOL bFullSuccess = res == 0 && !sfo.fAnyOperationsAborted;
Which horribly fails on a folder located on a USB flash drive, i.e. it is permanently deleted in despite of the FOF_ALLOWUNDO
flag.
So whether I'm not doing something right, or SHFileOperation API is very wrong!
Any idea how to do what I outlined above?
EDIT: I implemented the IRecycleBinManager::WillRecycle
method as was suggested by @Denis Anisimov, but there's evidently more to it. Here's my C++ version. First interface definition for the method I need:
#if defined(__cplusplus) && !defined(CINTERFACE)
MIDL_INTERFACE("5869092D-8AF9-4A6C-AE84-1F03BE2246CC")
IRecycleBinManager : public IUnknown
{
public:
//function WillRecycle(const pszPath: LPCWSTR): HRESULT; stdcall;
virtual HRESULT STDMETHODCALLTYPE WillRecycle(
/* [string][in] */ __RPC__in LPCWSTR pszFile) = 0;
};
#endif
and then the call itself:
HRESULT hr;
CoInitializeEx(NULL, COINIT_DISABLE_OLE1DDE | COINIT_APARTMENTTHREADED);
// {4A04656D-52AA-49DE-8A09-CB178760E748}
const CLSID CLSID_RecycleBinManager = {0x4A04656D, 0x52AA, 0x49DE, {0x8A, 0x09, 0xCB, 0x17, 0x87, 0x60, 0xE7, 0x48}};
// {5869092D-8AF9-4A6C-AE84-1F03BE2246CC}
const IID IID_IRecycleBinManager = {0x5869092D, 0x8AF9, 0x4A6C, {0xAE, 0x84, 0x1F, 0x03, 0xBE, 0x22, 0x46, 0xCC}};
IRecycleBinManager* pIRBM = NULL;
hr = CoCreateInstance(CLSID_RecycleBinManager, NULL, CLSCTX_INPROC_SERVER | CLSCTX_LOCAL_SERVER,
IID_IRecycleBinManager, (void**) &pIRBM);
// hr = SHCoCreateInstance(NULL, &CLSID_RecycleBinManager, NULL, IID_IRecycleBinManager, (void **)&pIRBM);
if (SUCCEEDED(hr))
{
hr = pIRBM->WillRecycle(L"C:\\test del"); //Crashes
pIRBM->Release();
}
Unfortunately I'm getting this error on the line where I'm supposed to call WillRecycle
method:
Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.
Every drive has its own Recycle Bin. And when you delete file from drive С: it should be moved to Recycle Bin on drive С:. When you delete file from USB drive it should be moved to Recycle Bin on USB drive. But when USB drive has no Recycle Bin then file is permanently deleted. This is default Windows behavior.
FOF_ALLOWUNDO flag is RECOMMENDATION only. MSDN says about FOF_ALLOWUNDO flag:
So there is no any error when Windows permanently deleted files even when you use FOF_ALLOWUNDO flag.
The only way I see is to check presence of Recycle Bin on drive with SHQueryRecycleBin function (as pointed by Alex Farber in comment) before delete operation. But even if Recycle Bin presents it is not full guaranty that file will be deleted to Recycle Bin. Recycle Bin has maximal limit of size and it can be already full.
UPDATE
You can use hack. You can emulate removing of file into Recycle Bin with you own code which will create all necessary system records in C:\$Recycle.Bin\UserSID folder. I tested this method on Windows 7 and it works correctly. It allows to ignore limitation of max size of Recycle Bin. Also it allows to move files from USB into Recycle Bin on any drive.
UPDATE 2
For Vista+ you can use undocumented interface IRecycleBinManager (Russian description can be found on webpage http://rcrrad.com/2010/10/14/bitbucket-interfaces/):
You can check the possibility of deletion of the file into Recycle Bin with the following code:
UPDATE 3
Also you can try the following code for delete oject into Recycle Bin:
I was able to come up with a solution to all 3 of my original points/requests.
In a nutshell, one needs to use the IFileOperation interface and implement IFileOperationProgressSink in it.
Here's full code sample and explanation for that.
EDIT: Ok, there's more to it. The method I posted above doesn't cover all bases :(