views:

1361

answers:

5

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?

+13  A: 

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.

John Topley
Another link: http://www.jroller.com/ethdsy/entry/send_to_recycle_bin
drye
This works like a charm! You can also move to Mac OS X Trash.
Paul Lammertsma
+2  A: 

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.

drye
+1  A: 

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.

Guðmundur Bjarni
+1  A: 

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.

Jesper
A: 

Here's the correct link to the example for using SHFileOperation to send a file to the recycle bin.

truist