开发者

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

开发者 https://www.devze.com 2023-01-16 15:35 出处:网络
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 possib

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?


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");
        }
    }
}


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

0

精彩评论

暂无评论...
验证码 换一张
取 消