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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/462110/
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-09-15 11:51:43  来源:igfitidea点击:

Acquiring drive names (as opposed to drive letters) in Java

javawindows

提问by Mattijs

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

在我的 Windows 机器上,我的主硬盘驱动器有字母 C: 和名称“本地磁盘”。

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.

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

Has anyone tried this before?

有没有人试过这个?

采纳答案by Mattijs

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

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

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

虽然它并不完美,但它会让你得到这个名字。从文档:

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.

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

回答by RealHowTo

Actually to get the drive name (ex. Local Disk) you need to use getSystemTypeDescription. getSystemDisplayName returns the volume name.

实际上要获取驱动器名称(例如本地磁盘),您需要使用 getSystemTypeDescription。getSystemDisplayName 返回卷名称。

import java.io.File;
import java.util.Arrays;
import java.util.List;
import javax.swing.filechooser.FileSystemView;

public class Test2 {
    public static void main(String args[]){

      List <File>files = Arrays.asList(File.listRoots());
      for (File f : files) {
        String s1 = FileSystemView.getFileSystemView().getSystemDisplayName (f);
        String s2 = FileSystemView.getFileSystemView().getSystemTypeDescription(f);
        System.out.println("getSystemDisplayName : " + s1);
        System.out.println("getSystemTypeDescription : " + s2);
      }
      /* output (French WinXP)

          getSystemDisplayName : 
          getSystemTypeDescription : Disquette 3? pouces

          getSystemDisplayName : REGA1 (C:)
          getSystemTypeDescription : Disque local

          getSystemDisplayName : 
          getSystemTypeDescription : Lecteur CD

          getSystemDisplayName : My Book (F:)
          getSystemTypeDescription : Disque local
      */
    }
}

回答by finnw

Using WMI(via JACOBor com4j) is another alternative.

使用WMI(通过JACOBcom4j)是另一种选择。

FileSystemView.getSystemDisplayNamedoes not give you the raw volume label. It is a combination of the drive letter and volume label, with a default in case the label has not been set.

FileSystemView.getSystemDisplayName不给你原始卷标。它是驱动器号和卷标的组合,如果未设置标签,则为默认值。

WMI will give you the raw volume label, plus some other info such is whether the drive is removable (surprisingly FileSystemView.isFloppyDrive()does not tell you this; It does literally mean "is it a floppy disk.")

WMI 将为您提供原始卷标,以及其他一些信息,例如驱动器是否可移动(令人惊讶的FileSystemView.isFloppyDrive()是没有告诉您这一点;它的字面意思是“它是软盘吗”。)