使用Java查找USB_FlashDrive的产品ID和供应商ID

时间:2021-08-03 07:31:55

I need help to find the product and vendor ID of USB_Flash-drive, I used the following code but it won't work,

我需要帮助才能找到USB_Flash驱动器的产品和供应商ID,我使用了以下代码,但它不起作用,

public static String getProductId() {
        // TODO Auto-generated method stub
        String result = "";
        try {
            File file = File.createTempFile("realhowto", ".vbs");
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);

            String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                    + "Set colItems = objWMIService.ExecQuery _ \n"
                    + "   (\"Select * from Win32_USBHub WHERE\"\\\\.\\PHYSICALDRIVE0\"\") \n"
                    + "For Each objItem in colItems \n"
                    + "    Wscript.Echo objItem.PNPDeviceID \n"
                    + "    exit for  ' do the first cpu only! \n" + "Next \n";
            fw.write(vbs);
            fw.close();
            Process p = Runtime.getRuntime().exec(
                    "cscript //NoLogo " + file.getPath());
            BufferedReader input = new BufferedReader(new InputStreamReader(
                    p.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                result += line;
            }
            input.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result.trim();
    }

In this above code i use this line (\"Select * from Win32_USBHub\") \n" instead of (\"Select * from Win32_USBHub WHERE\"\\\\.\\PHYSICALDRIVE0\"\") \n", which give the Product and Vendor id of my Dvd_Drive. Here what should i do for the correct result.

在上面的代码中我使用这一行(\“Select * from Win32_USBHub \”)\ n“而不是(\”Select * from Win32_USBHub WHERE \“\\\\。\\ PHYSICALDRIVE0 \”\“)\ n”,它给出了我的Dvd_Drive的产品和供应商ID。这里我该怎么做才能得到正确的结果。

1 个解决方案

#1