Java is the key here. I need to be able to delete files but users expect to be able to "undelete" from the recycle bin. As far as I can tell this isn't possible. Anyone know otherwise?
For various reasons Windows has no concept of a folder that simply corresponds to the Recycle Bin.
The correct way is to use JNI to invoke the Windows SHFileOperation
API, setting the FO_DELETE
flag in the SHFILEOPSTRUCT
structure.
- SHFileOperation documention
- Java example for copying a file using SHFileOperation (the Recycle Bin link in the same article doesn't work)
I have found this RFE on suns site: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5080625
This tells me there is not a native java way to do this. and as @John Topley just posted the only solution is a JNI call.
As John Topley suggests, you must do this with a native operation. In case you don't want to get your hands dirty with some JNI, you could use a library called Java Native Access to do the native calls.
See the fileutil incubator project (part of the Java Desktop Integration Components project):
This incubator project is created to host those file utility functionalities, most of which are extensions to the java.io.File class in J2SE. There are frequent requests from Java developers for such features like: sending a file to trash bin, checking free disk space, accessing file attributes etc. This project addresses such frequently requested APIs.
Note, this should work not only on Windows, but on other platforms (Linux, Mac OS X) as well.
Here's the correct link to the example for using SHFileOperation to send a file to the recycle bin.