How to close a windows explorer?

无人久伴 提交于 2019-12-23 03:37:24

问题


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:


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



来源:https://stackoverflow.com/questions/19615424/how-to-close-a-windows-explorer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!