Java on Windows: how to delete a file to trash (using JNA)
Asked Answered
A

2

5

I'm not experiences with Windows API at all, so please excuse my ignorance.

I want to delete files to the trash. How to do that using JNA and how to detect if this would not be possible, e.g., because the files are located on a network share?

Asperges answered 14/9, 2010 at 13:52 Comment(0)
R
3

Use com.sun.jna.platform.win32.W32FileUtils, which has moveToTrash and hasTrash methods defined.

Ranaerancagua answered 1/10, 2010 at 12:22 Comment(1)
where does this package exist ?Ejectment
H
9

Use com.sun.jna.platform.FileUtils instead of com.sun.jna.platform.win32.W32FileUtils directly.

import java.io.File;
import java.io.IOException;

import com.sun.jna.platform.FileUtils;

public class MoveToTrash {

    public static void main(String[] args){
        FileUtils fileUtils = FileUtils.getInstance();
        if (fileUtils.hasTrash()) {
            try {
                fileUtils.moveToTrash( new File[] {new File("c:/temp/dummy.txt") });                
            }
            catch (IOException ioe) {
                ioe.printStackTrace();
            }
        }
        else {
            System.out.println("No Trash available");
        }
    }
}
Hotblooded answered 6/10, 2013 at 20:38 Comment(0)
R
3

Use com.sun.jna.platform.win32.W32FileUtils, which has moveToTrash and hasTrash methods defined.

Ranaerancagua answered 1/10, 2010 at 12:22 Comment(1)
where does this package exist ?Ejectment

© 2022 - 2024 — McMap. All rights reserved.