java 枚举外部驱动器

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

Enumerate external drives

javac++filesystemsdrivers

提问by Asaf David

In java, you can use File.listRoots()to get all drives in the system.

在java中,您可以使用File.listRoots()获取系统中的所有驱动器。

I'm looking to get only the external drives, i.e. USB drives, external hard disks, optical drives, floppy, etc.

我只想获得外部驱动器,即 USB 驱动器、外部硬盘、光驱、软盘等。

Is there any way to do it in java? If not, native C++ code would be good as well. In that case, I need both Windows and Linux code.

有没有办法在java中做到这一点?如果没有,本机 C++ 代码也会很好。在这种情况下,我需要 Windows 和 Linux 代码。

回答by Zaki

To get file system info, use something like:

要获取文件系统信息,请使用以下内容:

import java.io.*;
import javax.swing.filechooser.*;

public class DriveTypeInfo
{
  public static void main(String[] args)
  {
      System.out.println("File system roots returned by   FileSystemView.getFileSystemView():");
      FileSystemView fsv = FileSystemView.getFileSystemView();
      File[] roots = fsv.getRoots();
      for (int i = 0; i < roots.length; i++)
      {
        System.out.println("Root: " + roots[i]);
      }

      System.out.println("Home directory: " + fsv.getHomeDirectory());

      System.out.println("File system roots returned by File.listRoots():");

      File[] f = File.listRoots();
      for (int i = 0; i < f.length; i++)
      {
        System.out.println("Drive: " + f[i]);
        System.out.println("Display name: " + fsv.getSystemDisplayName(f[i]));
        System.out.println("Is drive: " + fsv.isDrive(f[i]));
        System.out.println("Is floppy: " + fsv.isFloppyDrive(f[i]));
        System.out.println("Readable: " + f[i].canRead());
        System.out.println("Writable: " + f[i].canWrite());
      }
   }
}

Also see this, thisand thisquestion.

另请参阅问题。

回答by Christopher Wilson

You could use WMI or look into

您可以使用 WMI 或查看

GetRawInputDeviceList()

GetRawInputDeviceInfo()

GetRawInputDeviceList()

GetRawInputDeviceInfo()

This should get you started

这应该让你开始

C++

C++

http://cboard.cprogramming.com/windows-programming/114294-getting-list-usb-devices-listed-system.html

http://cboard.cprogramming.com/windows-programming/114294-getting-list-usb-devices-listed-system.html

Java

爪哇

http:// forums.java.net/jive/thread.jspa?threadID=37942

http://forums.java.net/jive/thread.jspa?threadID=37942