Skip to Main Content

Java HotSpot Virtual Machine

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Sending a file to the Windows recycle bin.

843829Jul 29 2003 — edited Aug 1 2003
Hi all,

I've been trying to write a function that sends a file, given the fully-qualified path, to the Recycle Bin. (To those who think you can just copy the file to "<logical-drive-name>\RECYCLER", you can't). I've posted my attempt below. The problem is, the file is not sent to the bin, and it's still stuck in the same directory. Essentially, the function doesn't work. My question is: what am I doing wrong?
JNIEXPORT void JNICALL Java_JNative_sendFileToRecycleBin (JNIEnv *env, jclass cls, jstring fileName)
{
        LPCTSTR src;
        HANDLE file;
        SHFILEOPSTRUCT opFile;
        
        src = (LPTSTR)(*env)->GetStringUTFChars (env, fileName, NULL);
        
        file = CreateFile (src, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
        
        // Checks if file exists.
        if (file == INVALID_HANDLE_VALUE)
        {
                (*env)->ReleaseStringUTFChars (env, fileName, src);
                (*env)->ThrowNew (env, (*env)->FindClass (env, "java/io/IOException"),
                        "Error while opening file.");
                return;
        }
        CloseHandle (file);

        opFile.wFunc = FO_DELETE;
        opFile.pFrom = src;
        opFile.fFlags = FOF_ALLOWUNDO;
       
        if (!SHFileOperation (&opFile))
        {
                (*env)->ReleaseStringUTFChars (env, fileName, src);
                (*env)->ThrowNew (env, (*env)->FindClass (env, "java/io/IOException"),
                        "Error while opening file."); 
        }
        
        (*env)->ReleaseStringUTFChars (env, fileName, src);
}
Thanks in advance!
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 29 2003
Added on Jul 29 2003
3 comments
204 views