简体   繁体   English

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

[英]Acquiring drive names (as opposed to drive letters) in Java

On my Windows machine, my main hard drive has the letter C: and the name "Local disk". 在我的Windows机器上,我的主硬盘驱动器上有字母C:和名称“Local disk”。

To list the drive letters in Java on Windows, the File object has the static listRoots() method. 要在Windows上列出Java中的驱动器号,File对象具有静态listRoots()方法。 But I can't find a way to acquire the drive names (as opposed to the drive letters) on Windows. 但我无法找到一种方法来获取Windows上的驱动器名称(而不是驱动器号)。

Has anyone tried this before? 有人曾尝试过这个吗?

Ah yes, you need to get the FileSystemView object and use getSystemDisplayName . 是的,您需要获取FileSystemView对象并使用getSystemDisplayName (I once implemented a Filesystem browser in Java). (我曾经用Java实现了一个Filesystem浏览器)。

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类获取信息。

Actually to get the drive name (ex. Local Disk) you need to use getSystemTypeDescription. 实际上,要获取驱动器名称(例如本地磁盘),您需要使用getSystemTypeDescription。 getSystemDisplayName returns the volume name. 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
      */
    }
}

Using WMI (via JACOB or com4j ) is another alternative. 使用WMI (通过JACOBcom4j )是另一种选择。

FileSystemView.getSystemDisplayName does not give you the raw volume label. FileSystemView.getSystemDisplayName不提供原始卷标。 It is a combination of the drive letter and volume label, with a default in case the label has not been set. 它是驱动器号和卷标的组合,默认情况下未设置标签。

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()没有告诉您;它确实意味着“它是否是软盘。”)

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

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