获取cup和硬盘的id号码

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Management ;//得独立添加引用要不然编译时找不到Management

namespace WindowsApplication1
{
 /// <summary>
 /// Summary description for Form1.
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.Label label2;
  private System.Windows.Forms.Label label3;
  private System.Windows.Forms.Label label4;
  private System.Windows.Forms.Label label5;
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Required for Windows Form Designer support
   //
   InitializeComponent();

   //
   // TODO: Add any constructor code after InitializeComponent call
   //
  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows Form Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.button1 = new System.Windows.Forms.Button();
   this.label1 = new System.Windows.Forms.Label();
   this.label2 = new System.Windows.Forms.Label();
   this.label3 = new System.Windows.Forms.Label();
   this.label4 = new System.Windows.Forms.Label();
   this.label5 = new System.Windows.Forms.Label();
   this.SuspendLayout();
   //
   // button1
   //
   this.button1.Location = new System.Drawing.Point(24, 24);
   this.button1.Name = "button1";
   this.button1.TabIndex = 0;
   this.button1.Text = "button1";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   //
   // label1
   //
   this.label1.Location = new System.Drawing.Point(24, 104);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(240, 23);
   this.label1.TabIndex = 1;
   this.label1.Text = "label1";
   //
   // label2
   //
   this.label2.Location = new System.Drawing.Point(24, 134);
   this.label2.Name = "label2";
   this.label2.Size = new System.Drawing.Size(240, 23);
   this.label2.TabIndex = 2;
   this.label2.Text = "label2";
   //
   // label3
   //
   this.label3.Location = new System.Drawing.Point(24, 164);
   this.label3.Name = "label3";
   this.label3.Size = new System.Drawing.Size(240, 23);
   this.label3.TabIndex = 3;
   this.label3.Text = "label3";
   //
   // label4
   //
   this.label4.Location = new System.Drawing.Point(24, 194);
   this.label4.Name = "label4";
   this.label4.Size = new System.Drawing.Size(240, 23);
   this.label4.TabIndex = 4;
   this.label4.Text = "label4";
   //
   // label5
   //
   this.label5.Location = new System.Drawing.Point(24, 224);
   this.label5.Name = "label5";
   this.label5.Size = new System.Drawing.Size(240, 23);
   this.label5.TabIndex = 5;
   this.label5.Text = "label5";
   //
   // Form1
   //
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
   this.ClientSize = new System.Drawing.Size(292, 273);
   this.Controls.Add(this.label5);
   this.Controls.Add(this.label4);
   this.Controls.Add(this.label3);
   this.Controls.Add(this.label2);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.button1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.ResumeLayout(false);

  }
  #endregion

  /// <summary>
  /// The main entry point for the application.
  /// </summary>
  [STAThread]
  static void Main()
  {
   Application.Run(new Form1());
  }

  private void button1_Click(object sender, System.EventArgs e)
  {
   
   ManagementObjectSearcher my = new ManagementObjectSearcher("SELECT * FROM Win32_BaseBoard");
   foreach(ManagementObject share in my.Get())
   {
    label1.Text= "主板制造商:" + share["Manufacturer"].ToString();
    label2.Text= "产品:" + share["Product"].ToString();
    label3.Text= "主板序列号:" + share["SerialNumber"].ToString();
   }
    
   //得到cpu序列号
   string strCPUNo = "";
   ManagementClass cimObject = new ManagementClass("Win32_Processor");
   ManagementObjectCollection mocHard = cimObject.GetInstances();
   foreach(ManagementObject moHard in mocHard)
   {
    strCPUNo = moHard.Properties["ProcessorId"].Value.ToString();
         
   }
   label4.Text = "CPU序列号为:" + strCPUNo;
    
   //硬盘序列号
   ManagementObject m_objDisk = new ManagementObject("win32_logicaldisk.deviceid=/"c:/"");
   string strSN = (string)m_objDisk.GetPropertyValue("VolumeSerialNumber");
   label5.Text = "硬盘序列号为:" + strSN ;

  }
 }
}
 

获取本机CPU序列号和硬盘序列号需要使用系统相关的API,因此代码会有一定的平台依赖性。以下是获取这两个序列号的Java代码示例: 获取CPU序列号: ```java public static String getCpuSerial() { String result = ""; try { Process process = Runtime.getRuntime().exec(new String[] { "wmic", "cpu", "get", "ProcessorId" }); process.getOutputStream().close(); Scanner scanner = new Scanner(process.getInputStream()); scanner.next(); result = scanner.next(); scanner.close(); } catch (IOException e) { e.printStackTrace(); } return result.trim(); } ``` 获取硬盘序列号: ```java public static String getHardDiskSerial() { String result = ""; try { File file = File.createTempFile("realhowto", ".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(\"C\")\n" + "Wscript.Echo objDrive.SerialNumber"; fw.write(vbs); fw.close(); Process process = Runtime.getRuntime().exec("cscript //NoLogo " + file.getPath()); process.getOutputStream().close(); Scanner scanner = new Scanner(process.getInputStream()); result = scanner.next(); scanner.close(); } catch (IOException e) { e.printStackTrace(); } return result.trim(); } ``` 需要注意的是,以上代码只是演示了如何获取CPU序列号和硬盘序列号,实际应用中还需要进行一些错误处理和适配不同的操作系统。同时,获取硬盘序列号需要在Windows系统中运行。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值