简体   繁体   English

在Java中查找所有驱动器号

[英]Find all drive letters in Java

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 there is no such thing on Windows. 对于UNIX衍生产品,我假设用户在强大的$ PATH变量中有文件,但在Windows上没有这样的东西。

I can safely assume the file is at most 2 levels deep into the filesystem, but I don't know on what drive it will be. 我可以放心地假设文件最多只有2级到文件系统,但我不知道它将在什么驱动器上。 I have to try all drives, but I can't figure out how to list all available drives (which have a letter assigned to it). 我必须尝试所有驱动器,但我无法弄清楚如何列出所有可用的驱动器(分配了一个字母)。

Any help? 有帮助吗?

EDIT: I know there is a %PATH% variable, but it is not as integrated as in UNIX systems. 编辑:我知道有一个%PATH%变量,但它不像UNIX系统那样集成。 For instance, the application I'm looking for is OpenOffice. 例如,我正在寻找的应用程序是OpenOffice。 Such software would not be in %PATH%, typically. 通常,此类软件不会以%PATH%为单位。

http://docs.oracle.com/javase/7/docs/api/java/io/File.html#listRoots() 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:-) 谷歌:列表驱动java,首先命中:-)

Looking "everywhere" can be very messy. 看起来“无处不在”可能非常混乱。

Look at a CD-rom drive, and it spins up. 看看CD-ROM驱动器,它会旋转。 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. 也许(对于Windows机器)你应该只看一下开始菜单。 If nothing there points at OOo, it's probably not installed. 如果在OOo没有任何指示,则可能没有安装。 If it is, the user is probably an advanced user, that will have no problems pointing out the location manually. 如果是,用户可能是高级用户,则手动指出位置没有问题。

Windows does indeed have a PATH environment variable. Windows确实有一个PATH环境变量。 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. 它有一个与Unix不同的语法,因为它使用分号(;)作为分隔符而不是冒号(:),你必须注意可能包含空格的引用字符串。 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. 如果这个其他程序的安装程序将自己的目录添加到PATH环境变量,那么您可以依赖它。 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. 但是,正如您所提到的,Windows安装程序通常不需要将应用程序路径添加到PATH,因为它们会安装开始菜单快捷方式或其他内容。

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. 对于Java中的驱动器号,一种方法是尝试所有这些,只有最多24个(C到Z)才有用。 Or, you could shell out and run "net use" and parse the results, though that is a bit messier. 或者,你可以弹出并运行“net use”并解析结果,尽管这有点麻烦。

Use JNI. 使用JNI。 This is perfect for c++ code. 这非常适合c ++代码。 Not only you can list all drives but also get the corresponding drive type (removable,local disk, or cd-rom,dvd-rom...etc) 您不仅可以列出所有驱动器,还可以获得相应的驱动器类型(可移动,本地磁盘或CD-ROM,DVD-ROM等)

Of course there is a PATH environment variable in Windows . 当然,Windows中有一个PATH环境变量。

%PATH% This variable contains a semicolon-delimited list of directories in which the command interpreter will search for executable files. %PATH%此变量包含以分号分隔的目录列表,命令解释程序将在其中搜索可执行文件。 Equivalent to the UNIX $PATH variable. 相当于UNIX $ PATH变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM