Java获得硬盘和主板的序列号代码

当你需要获得windows硬件细节时,java可能不是最好的工具。 简直无从下手,不过可以通过VBS脚本获得需要的数据,然后java调用VBS获得输出。

VBS脚本通过查询WMI来获得系统硬件信息。 我们需要Win32_BaseBoard类,更多可以参考 http://msdn2.microsoft.com/en-us/library/aa389273.aspx.

获得主板信息:

  1. import java.io.File;
  2. import java.io.FileWriter;
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. http://www.kmnk03.com/hxpfk/dzpz/309.html
  6. public class MiscUtils {
  7. private MiscUtils() { }
  8. public static String getMotherboardSN() {
  9. String result = "";
  10. try {
  11. File file = File.createTempFile("realhowto",".vbs");
  12. file.deleteOnExit();
  13. FileWriter fw = new java.io.FileWriter(file);
  14. http://www.kmnk03.com/hxpfk/npx/310.html
  15. String vbs =
  16. "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
  17. + "Set colItems = objWMIService.ExecQuery _ \n"
  18. + " (\"Select * from Win32_BaseBoard\") \n"
  19. + "For Each objItem in colItems \n"
  20. + " Wscript.Echo objItem.SerialNumber \n"
  21. + " exit for ' do the first cpu only! \n"
  22. + "Next \n";
  23. http://www.kmnk03.com/hxpfk/npx/311.html
  24. fw.write(vbs);
  25. fw.close();
  26. Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
  27. BufferedReader input =
  28. new BufferedReader
  29. (new InputStreamReader(p.getInputStream()));
  30. String line;
  31. while ((line = input.readLine()) != null) {
  32. result += line;
  33. }
  34. input.close();
  35. }http://www.kmnk03.com/hxpfk/py/312.html
  36. catch(Exception e){
  37. e.printStackTrace();
  38. }
  39. return result.trim();
  40. }
  41. public static void main(String[] args){
  42. String cpuId = MiscUtils.getMotherboardSN();
  43. javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
  44. null, cpuId, "Motherboard serial number",
  45. javax.swing.JOptionPane.DEFAULT_OPTION);
  46. }
  47. }http://www.kmnk03.com/hxpfk/py/313.html
复制代码

获得硬盘序列号:

  1. import java.io.File;http://www.kmnk03.com/hxpfk/xmz/314.html
  2. import java.io.FileWriter;
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. http://www.kmnk03.com/hxpfk/xmz/315.html
  6. public class DiskUtils {
  7. private DiskUtils() { }
  8. public static String getSerialNumber(String drive) {
  9. String result = "";
  10. try {
  11. File file = File.createTempFile("realhowto",".vbs");
  12. file.deleteOnExit();
  13. FileWriter fw = new java.io.FileWriter(file);
  14. http://www.kmnk03.com/hxpfk/ylb/316.html
  15. String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
  16. +"Set colDrives = objFSO.Drives\n"
  17. +"Set objDrive = colDrives.item(\"" + drive + "\")\n"
  18. +"Wscript.Echo objDrive.SerialNumber"; // see note
  19. fw.write(vbs);
  20. fw.close();
  21. Process p = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath());
  22. BufferedReader input =
  23. new BufferedReader
  24. (new InputStreamReader(p.getInputStream()));
  25. String line;
  26. while ((line = input.readLine()) != null) {
  27. result += line;
  28. }http://www.kmnk03.com/hxpfk/ylb/317.html
  29. input.close();
  30. }
  31. catch(Exception e){
  32. e.printStackTrace();
  33. }http://www.kmnk03.com/hxpfk/ylb/319.html
  34. return result.trim();
  35. }
  36. kmnk03.com   kmnk01.com
  37. public static void main(String[] args){
  38. String sn = DiskUtils.getSerialNumber("C");
  39. javax.swing.JOptionPane.showConfirmDialog((java.awt.Component)
  40. null, sn, "Serial Number of C:",
  41. javax.swing.JOptionPane.DEFAULT_OPTION);
  42. }www.kmnk01.com
  43. www.kmnk03.com
  44. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值