13

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

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.

Has anyone tried this before?

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Mattijs
  • 1,909
  • 3
  • 19
  • 28

3 Answers3

12

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:

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.

  • How to get Disk name. I am not talking about volume lable) But the disk name like HP CD Drive, Samsung SSD, StoreJet etc – Ashish Apr 25 '16 at 11:35
11

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

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
      */
    }
}
RealHowTo
  • 34,977
  • 11
  • 70
  • 85
  • What is the system type description for an USB key? Does it differ from Windows XP to Windows 7? – Maxbester May 24 '13 at 11:35
  • 1
    @Maxbester : For a USB key, getSystemTypeDescription() will return "Disque amovible" (french) or "Removable disk" (english). – RealHowTo May 24 '13 at 21:41
  • Thanks. This is strange there is no way to get those strings from a global system property... – Maxbester May 27 '13 at 07:29
  • @RealHowTo you are great man. You've solved my problem. Can you please tell is there any way to get the hardware details of USB device except using JNI/VBScript cause I got the hardware details using VBScript. But its little tricky do you have any small code snippet like the above one to get hardware details like id,vendor name etc. – Vighanesh Gursale Aug 15 '13 at 09:46
0

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

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.

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

finnw
  • 47,861
  • 24
  • 143
  • 221