Open / Close Disk Reader (Vbs)

0

I'm using a code to "open / close the disk drawer" however it creates a file on the desktop and would like to sort out some other way to resolve it.

package controller;

import java.awt.Desktop;
import java.io.File;
import java.io.PrintWriter;

public class disc_1 {

    public static void main(String[] args) {
        try {
            //********Start VBScript code to open cd tray************  
            String a = "Set oWMP = CreateObject(\"WMPlayer.OCX\")" + "\n"
                    + "Set colCDROMs = oWMP.cdromCollection" + "\n"
                    + "For d = 0 to colCDROMs.Count - 1" + "\n"
                    + "colCDROMs.Item(d).Eject" + "\n"
                    + "Next" + "\n"
                    + "set owmp = nothing" + "\n"
                    + "set colCDROMs = nothing" + "\n"
                    + "wscript.Quit(0)";
            //********End VBScript code to open cd tray************  

            File myCdTrayOpener = new File("OpenCdTray.vbs");
            PrintWriter pw = new PrintWriter(myCdTrayOpener);
            pw.print(a);
            pw.flush();
            pw.close();
            Desktop.getDesktop().open(myCdTrayOpener);
            myCdTrayOpener.deleteOnExit();

        } catch (Exception exception) {
            exception.printStackTrace();

        } finally {

            try {
                Thread.sleep(2500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            try {
                //********Start VBScript code to open cd tray************  
                String a = "Set oWMP = CreateObject(\"WMPlayer.OCX\")" + "\n"
                        + "Set colCDROMs = oWMP.cdromCollection" + "\n"
                        + "For d = 0 to colCDROMs.Count - 1" + "\n"
                        + "colCDROMs.Item(d).Eject" + "\n"
                        + "colCDROMs.Item(d).Eject" + "\n"
                        + "Next" + "\n"
                        + "set owmp = nothing" + "\n"
                        + "set colCDROMs = nothing" + "\n"
                        + "wscript.Quit(0)";
                //********End VBScript code to open cd tray************  

                File myCdTrayOpener = new File("OpenCdTray.vbs");
                PrintWriter pw = new PrintWriter(myCdTrayOpener);
                pw.print(a);
                pw.flush();
                pw.close();
                Desktop.getDesktop().open(myCdTrayOpener);
                myCdTrayOpener.deleteOnExit();

            } catch (Exception exception) {
                exception.printStackTrace();
            }
        }

    }

}
    
asked by anonymous 31.03.2015 / 16:37

1 answer

2

One of the class constructors File allows you to send as an argument the directory where the file should be created. With that in mind, just pass a String containing the directory path. For example:

// Estando em ambiente Windows, 'diretorio' terá algo como "C:\Users\FULANO"
String diretorio = System.getProperty("user.home");

File myCdTrayOpener = new File(diretorio, "OpenCdTray.vbs"); 

In this case, the idea of using user.home came by calling the deleteOnExit . Since the file will be deleted when the virtual machine is finished, I do not think you have any problems keeping the file temporarily in that location.

You can also get the program data directory , if any.

    
31.03.2015 / 18:59