USB闪存驱动器制造商序列号



如何在java中检索USB闪存驱动器的制造商序列号?

我看这个

如何获得USB闪存驱动器的制造商序列号?

我不认为有一个纯Java解决方案可以跨多个平台工作。

您可能需要"抓取"外部命令的输出。(或者弄清楚他们在做什么,并在本地代码库中复制它…)

在Linux上

:

  • "lsusb"命令的输出包括序列号。(检查手动输入)
在Windows:

  • 第三方命令"usp64"one_answers"USBDeview"做到这一点:参见http://ashfaqshinwary.wordpress.com/tag/how-to-find-usb-serial-number-from-windows/。

  • 显然,您可以使用wmic来获取序列号,尽管您可能需要应用Microsoft修复以使其工作:https://superuser.com/questions/600394/get-usb-key-manufacturer-serial-number


@FoggyDay已经找到了一些第三方库来支持这个功能:

  • jUSB显然只适用于GNU/Linux

  • usb4java支持更广泛的平台


建议使用Files.getFileStore(path).getAttribute("volume:vsn")的评论是不正确的,我认为。Java源代码暗示它返回的是由Windows分配的卷序列号,而不是制造商提供的序列号。相关代码如下:

jUSB库允许您从usb.core.Configuration中读取。

usb4java -另一个较新的Java库-可能也支持相同的。

此方法可帮助您读取USB的数

    public static String getSerialNumber() {
    String command = "powershell.exe Get-WmiObject Win32_DiskDrive | Select-Object SerialNumbern";
    String serialNumber = null;
    try {
        Process powerShellProcess = Runtime.getRuntime().exec(command);
        powerShellProcess.getOutputStream().close();
        BufferedReader stdout = new BufferedReader(new InputStreamReader(powerShellProcess.getInputStream()));
        String line;
        while ((line = stdout.readLine()) != null) {
            System.out.println("PowerShell output: " + line);
            if (!line.trim().isEmpty()) {
                serialNumber = line.trim();
            }
        }
        stdout.close();
        BufferedReader stderr = new BufferedReader(new InputStreamReader(powerShellProcess.getErrorStream()));
        while ((line = stderr.readLine()) != null) {
            System.out.println(line);
        }
        stderr.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return serialNumber;
}

最新更新