在 Java 中查找所有驱动器号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/51320/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 07:40:14  来源:igfitidea点击:

Find all drive letters in Java

javawindows

提问by wvdschel

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. 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).

我可以安全地假设文件在文件系统中最多有 2 个级别,但我不知道它将在哪个驱动器上。我必须尝试所有驱动器,但我不知道如何列出所有可用驱动器(分配了一个字母)。

Any help?

有什么帮助吗?

EDIT:I know there is a %PATH% variable, but it is not as integrated as in UNIX systems. For instance, the application I'm looking for is OpenOffice. Such software would not be in %PATH%, typically.

编辑:我知道有一个 %PATH% 变量,但它不像在 UNIX 系统中那样集成。例如,我正在寻找的应用程序是 OpenOffice。通常,此类软件不会位于 %PATH% 中。

采纳答案by Tony BenBrahim

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,第一次命中:-)

回答by Frank Krueger

Of course there is a PATHenvironment 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. Equivalent to the UNIX $PATH variable.

%PATH%此变量包含一个以分号分隔的目录列表,命令解释器将在其中搜索可执行文件。等效于 UNIX $PATH 变量。

回答by Greg Hewgill

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.

Windows 确实有一个 PATH 环境变量。它与 Unix 的语法不同,因为它使用分号 (;) 作为分隔符而不是冒号 (:),您必须注意可能包含空格的带引号的字符串。但是,它就在那里。

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.

如果这个其他程序的安装程序将自己的目录添加到 PATH 环境变量中,那么您可以依赖它。但是,正如您所提到的,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. Or, you could shell out and run "net use" and parse the results, though that is a bit messier.

对于 Java 中的驱动器号,一种方法是全部尝试,最多只有 24 个(C 到 Z)有用。或者,您可以退出并运行“net use”并解析结果,尽管这有点混乱。

回答by myplacedk

Looking "everywhere" can be very messy.

看着“无处不在”可能会非常混乱。

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

看看 CD-rom 驱动器,它会旋转起来。那会很吵。

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.

也许(对于 Windows 机器)您应该查看开始菜单。如果 OOo 上没有任何内容,则它可能未安装。如果是,则用户可能是高级用户,手动指出位置没有问题。

回答by Defd

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)

使用 JNI。这非常适合 C++ 代码。您不仅可以列出所有驱动器,还可以获取相应的驱动器类型(可移动、本地磁盘或 cd-rom、dvd-rom...等)