lyh
7 小时以前 7126eab629d31beb5164b576a44b865a6a00f07c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package com.lxzn.system;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
 
/**
 * @Description:
 * @Author: zhangherong
 * @Date: Created in 2021/6/15 11:01
 * @Version: 1.0
 * @Modified By:
 */
public class MiscUtil {
    public static String getMotherboardSN(){
        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_BaseBoard\") \n"
//                    + "For Each objItem in colItems \n"
//                    + " Wscript.Echo objItem.SerialNumber \n"
//                    + " exit for ' do the first cpu only! \n" + "Next \n";
            String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
                    + "Set colItems = objWMIService.ExecQuery _ \n"
                    + "   (\"Select * from Win32_BaseBoard\") \n"
                    + "For Each objItem in colItems \n"
                    + "    Wscript.Echo objItem.SerialNumber \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();
    }
 
    public static void main(String[] args) {
        String cpuId = MiscUtil.getMotherboardSN();
        System.out.println(cpuId);
    }
}