Date: 2007dec29
Update: 2025jan13
Platform: win32
Language: C/C++
Q. Win32: Programmatically move a file to the Windows recycle bin
A. Here is a function that does just that.
(This is nice thing to do if a user of your program ever
requests that a file be deleted.)
// Delete by moving to reycle bin
BOOL RecycleFile(LPCSTR szFileIn)
{
char szFile[MAX_PATH + 2];
SHFILEOPSTRUCT op = { 0 };
lstrcpyn(szFile, szFileIn, sizeof(szFile));
szFile[lstrlen(szFile) + 1] = '\0'; // Extra terminating NUL required
op.wFunc = FO_DELETE;
op.pFrom = szFile;
op.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION | FOF_NOERRORUI |
FOF_SILENT; // Options set for no user interaction
return 0 == SHFileOperation(&op);
}
void ExampleUse() {
RecycleFile("c:\\temporary\\myfile.dat");
}