Find all drive letters in Java

前端 未结 5 869
花落未央
花落未央 2020-12-03 21:06

For a project I\'m working on. I need to look for an executable on the filesystem. For UNIX derivatives, I assume the user has the file in the mighty $PATH variable, but the

相关标签:
5条回答
  • 2020-12-03 21:36

    Of course there is a PATH environment variable in Windows.

    %PATH% This variable contains a semicolon-delimited list of directories in which the command interpreter will search for executable files. Equivalent to the UNIX $PATH variable.

    0 讨论(0)
  • 2020-12-03 21:38

    http://docs.oracle.com/javase/7/docs/api/java/io/File.html#listRoots()

    File[] roots = File.listRoots();
    for(int i = 0; i < roots.length ; i++)
        System.out.println("Root["+i+"]:" + roots[i]);
    

    google: list drives java, first hit:-)

    0 讨论(0)
  • 2020-12-03 21:38

    Use JNI. This is perfect for c++ code. Not only you can list all drives but also get the corresponding drive type (removable,local disk, or cd-rom,dvd-rom...etc)

    0 讨论(0)
  • 2020-12-03 21:40

    Windows does indeed have a PATH environment variable. It has a different syntax from the Unix one because it uses semicolon (;) as a separator instead of colon (:) and you have to watch for quoted strings that might contain spaces. But, it's there.

    If this other program's installer adds its own directory to the PATH environment variable, then you could rely on that. However, as you mention, Windows installers typically do not need to add the application path to the PATH because they install a start menu shortcut or something else instead.

    For drive letters in Java, one approach would be to try them all, there are only going to be at most 24 (C through Z) that are of any use. Or, you could shell out and run "net use" and parse the results, though that is a bit messier.

    0 讨论(0)
  • 2020-12-03 21:50

    Looking "everywhere" can be very messy.

    Look at a CD-rom drive, and it spins up. That can be very noisy.

    Look at a network drive, and it may be very slow. Maybe the server is down, and you may need to wait for minutes until it times out.

    Maybe (for Windows-machines) you should just look in the start-menu. If nothing there points at OOo, it's probably not installed. If it is, the user is probably an advanced user, that will have no problems pointing out the location manually.

    0 讨论(0)
提交回复
热议问题