Created
October 16, 2018 21:52
-
-
Save LeoFalco/2fa7188f87cd5351a0c9e071d5a1d4ee to your computer and use it in GitHub Desktop.
consultar serial HD
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public String consultarSerialHD(String drive) { | |
StringBuilder result = new StringBuilder(); | |
try { | |
File file = File.createTempFile("tmp", ".vbs"); | |
file.deleteOnExit(); | |
FileWriter fw = new 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"; | |
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.append(line); | |
} | |
input.close(); | |
} catch (Exception ignored) { | |
} | |
if (result.length() < 7) { | |
result = new StringBuilder("0135768901"); | |
} else if (result.length() == 8) { | |
result.insert(0, "00"); | |
} else if (result.length() == 9) { | |
result.insert(0, "0"); | |
} else if (result.length() > 10) { | |
result = new StringBuilder(result.substring(0, 10)); | |
} | |
return result.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment