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

时间:2011-07-29 13:49:39

标签: java vbscript

我需要帮助才能找到USB_Flash-drive的产品和供应商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();
    }

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