1.获取CPU序列号

(1)windows系统第一种获取方式

public class Test{
    public static void main(String[] args) throws IOException {
        System.out.println(getCPUSerial());
        System.exit(0);
    }
    
    /**获取本机CPU信息
     */
    private static String getCPUSerial() {
        String result = "";
        try {
            File file = File.createTempFile("tmp", ".vbs");//创建临时文件,路径为C:\Documents and Settings\Administrator\Local Settings\Temp
            file.deleteOnExit();
            FileWriter fw = new java.io.FileWriter(file);
            //是有vbs脚本语言,获取CPU唯一ID
            //表示程序出现运行时错误时,会继续运行,不中断
            StringBuilder sb = new StringBuilder("On Error Resume Next \r\n\r\n");
            //表示本机
            sb.append("strComputer = \".\"  \r\n");
            //使用GetObject函数获取本机信息赋值给objWMIService
            sb.append("Set objWMIService = GetObject(\"winmgmts:\" _ \r\n");
            sb.append("    & \"{impersonationLevel=impersonate}!\\\\\" & strComputer & \"\\root\\cimv2\") \r\n");
            sb.append("Set colItems = objWMIService.ExecQuery(\"Select * from Win32_Processor\")  \r\n ");
            //使用for循环取出CPU信息
            sb.append("For Each objItem in colItems\r\n " + "    Wscript.Echo objItem.ProcessorId  \r\n ");
            sb.append("    exit for  ' do the first cpu only! \r\n");
            sb.append("Next");

            fw.write(sb.toString());
            fw.close();
            Process p = Runtime.getRuntime().exec("cscript //NoLogo //T:10 " + file.getPath());
            BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line;
            while ((line = input.readLine()) != null) {
                result += line;
            }
            input.close();
            file.delete();
        } catch (Exception e) {
            e.fillInStackTrace();
        }
        return result;
    }
}
View Code

相关文章: