Find all drive letters in Java

前端 未结 5 889
花落未央
花落未央 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: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.

提交回复
热议问题