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

时间:2023-01-26 21:34:36

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中的驱动器号,文件对象具有静态listRoots()方法。但是我找不到一种方法来获取Windows上的驱动器名(而不是驱动器号)。

Has anyone tried this before?

有人试过吗?

3 个解决方案

#1


11  

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

#2


10  

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

实际上要获取驱动器名(ex. Local磁盘),您需要使用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
      */
    }
}

#3


0  

Using WMI (via JACOB or com4j) is another alternative.

使用WMI(通过JACOB或com4j)是另一种选择。

FileSystemView.getSystemDisplayName does 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 . isvelpydrive()没有告诉您这一点;它的字面意思是“这是软盘吗?”

#1


11  

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

#2


10  

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

实际上要获取驱动器名(ex. Local磁盘),您需要使用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
      */
    }
}

#3


0  

Using WMI (via JACOB or com4j) is another alternative.

使用WMI(通过JACOB或com4j)是另一种选择。

FileSystemView.getSystemDisplayName does 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 . isvelpydrive()没有告诉您这一点;它的字面意思是“这是软盘吗?”