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

来源:互联网 发布:英文版导航软件 编辑:程序博客网 时间:2024/04/29 22:12

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

获得硬盘序列号:

  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/xmz/314.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/xmz/315.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 BufferedReaderhttp://www.kmnk03.com/hxpfk/ylb/316.html
  24. (new InputStreamReader(p.getInputStream()));
  25. String line;
  26. while ((line = input.readLine()) != null) {
  27. result += line;
  28. }
  29. input.close();
  30. }
  31. catch(Exception e){
  32. e.printStackTrace();
  33. }http://www.kmnk03.com/hxpfk/ylb/317.html
  34. return result.trim();
  35. }
  36. http://www.kmnk03.com/hxpfk/ylb/319.html
  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. }kmnk03.com
  43. }www.kmnk03.com
0 0
原创粉丝点击