Identify drive letter or drive letter in autorun

0

I'm creating an application that should be run by DVD-R, and you also need to run background programs using the process.start("D:\Local do arquivo") command.

But there is my doubt: if the client has computer partitions, the disk location will not be D:\ , but F:\ .

How do I make the program run and another background program even though the drives are different?

    
asked by anonymous 19.01.2015 / 21:04

1 answer

2

If the application is on the same drive as the other application that will be fired, you can identify the unit of your application and hence form the full path of the second application.

For example, instead of setting the other application (in my example, application2.exe ) to D: just set your path relative to the root drive and create the full path from the root drive of the application that will run it:

    String caminhoApp = System.getProperty("user.dir");
    String unidadeApp = caminhoApp.substring(0, caminhoApp.indexOf(File.separator));

    System.out.println(unidadeApp);

    String nomeOutroApp = String.format("Local do arq%saplicativo2.exe", File.separator);
    String caminhoOutroApp = new File(unidadeApp ,nomeOutroApp).getPath();

    System.out.println(caminhoOutroApp);

If you do not know which drive the other application will be on, you can search for drives. For example:

public String localizaOutroApp(String caminhoRelativoARaiz) {
    File[] unidades = File.listRoots();
    for(int i = 0; i < unidades.length ; i++) {
        File arquivo = new File(unidades[i], caminhoRelativoARaiz);
        if (arquivo.exists()) {
            return arquivo.getPath();
        }
    }
    return null;
}

Passing to the above function the path of the other application relative to the root, it will return the full path of the other application no matter on which drive it was found. Example:

// se o existe "F:\Local do arquivo\aplicativo2.exe", é este caminho que será impresso.
System.out.println(localizaOutroApp("Local do arquivo\aplicativo2.exe"));

In the second example I used "\" instead of "File.separator" to make reading easier. Always use "File.separator" if you want portability between operating systems.

    
19.01.2015 / 21:32