在Java中获取驱动器名称(而不是驱动器号)

在Java中获取驱动器名称(而不是驱动器号)

问题描述:

在我的Windows机器上,我的主硬盘驱动器上有字母C:和名称Local disk。

On my Windows machine, my main hard drive has the letter C: and the name "Local disk".

要在Windows上列出Java中的驱动器号,File对象具有静态listRoots()方法。但是我找不到在Windows上获取驱动器名称(而不是驱动器号)的方法。

To list the drive letters in Java on Windows, the File object has the static listRoots() method. But I can't find a way to acquire the drive names (as opposed to the drive letters) on Windows.

有没有人尝试过这个?

是的,你需要获取FileSystemView对象并使用 getSystemDisplayName 。 (我曾经用Java实现了一个Filesystem浏览器。)

Ah yes, you need to get the FileSystemView object and use getSystemDisplayName. (I once implemented a Filesystem browser in Java).

虽然不是很完美,但它会得到你的名字。从文档中:

It's not perfect though but it will get you the name. From the documentation:


将在系统文件浏览器中显示的文件,目录或文件夹的名称。 Windows中的示例:M:\目录显示为CD-ROM(M :)默认实现从ShellFolder类获取信息。

Name of a file, directory, or folder as it would be displayed in a system file browser. Example from Windows: the "M:\" directory displays as "CD-ROM (M:)" The default implementation gets information from the ShellFolder class.