最近程序需要绑定U盘启动
找到的一个获得U盘序列号的程序,只支持window,已测win7 32bit,64bit
- /**
- * Description:得到U盘序列号
- * @author liuwei DateTime 2013-11-4 下午6:05:56
- * @param drive
- * @return
- */
- public static String getSerialNumber(String drive) {
- String result = "";
- try {
- File file = File.createTempFile("realhowto",".vbs");
- file.deleteOnExit();
- FileWriter fw = new java.io.FileWriter(file);
- String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
- +"Set colDrives = objFSO.Drives\n"
- +"Set objDrive = colDrives.item(\"" + drive + "\")\n"
- +"Wscript.Echo objDrive.SerialNumber"; // see note
- 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();
- }