How to close a windows explorer?

2019-05-30 13:40发布

I have a code that uses jDesktop to open a windows explorer interface when I clicked the button LOGIN and it's working right..

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    // TODO add your handling code here:
     Desktop desktop = Desktop.getDesktop();
    File dirToOpen;
    try {
        dirToOpen = new File("C://as//2010-0000-1");
        desktop.open(dirToOpen);
    } catch (IOException ex) {
        ex.getMessage();
    } catch (IllegalArgumentException iae) {
        System.out.println("File Not Found");
    }
}  

then now, my problem is when I click the button LOGOUT, the jDesktop windows explorer interface should also be closed... I dont know what codes to use....

1条回答
淡お忘
2楼-- · 2019-05-30 14:11

This is not so simple, they only chance you have is if you have a reference to the process in question. This is going to mean you're going to need to take more control over the process...This also means that it will only work on Windows...

I use the following code to show a specified file in Windows Explorer...

String path = file.getCanonicalPath();

ProcessBuilder pb = new ProcessBuilder("explorer.exe", "/select," + path);
pb.redirectError();
Process proc = pb.start();

Once you have access to the Process, you can try using Process#destory to try and terminate the process.

Launching the process should be done from a separate thread, so you don't get yourself all tied up in a block point, you should also consume the Process's output just incase it causes the process to stall.

ps- I don't have access to a Windows machine at the moment, so I'm not sure if Process#destory will work ;)

查看更多
登录 后发表回答