用C#获得计算机名称,IP地址,MAC地址以及CPU序列号

来源:互联网 发布:卡密软件刷q币软件下载 编辑:程序博客网 时间:2024/05/02 01:57

Visual C#是微软推出的下一代程序开发语言,是微软.NET 框架中的的一个重要组成部分,在推出Visual C#的过程中,微软还推出了与之相对应的一个软件开发包--.Net FrameWork SDK。此软件开发包里面封装了许多类、对象。Visual C#就是通过调用这些类、对象来实现许多比较强大的功能。
   
.Net FrameWork SDK中提供了功能强大名称空间,比如:System.NetSystem.Net.Socket, System.Management, System.DirectoryServices;本人就是利用这些名称空间中封装的类和对象来读取本地计算机名称和机器中所有的IP地址,还有机器的MAC地址,以及机器的CPU序列号.我把测出来的结果和系统信息对照一下,准确无误.呵呵…

下面就是测试的源代码,拿出来和大家分享!!!

 

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using System.Management;

using System.DirectoryServices;

using System.Net;

using System.Net.Sockets;

 

namespace MAC_IP_Computer_name

{

     /// <summary>

     /// Form1 的摘要说明。

     /// </summary>

     public class Form1 : System.Windows.Forms.Form

     {

         private System.Windows.Forms.Label label1;

         private System.Windows.Forms.Label label2;

         private System.Windows.Forms.Label label3;

         private System.Windows.Forms.TextBox txtMac;

         private System.Windows.Forms.TextBox txtIP;

         private System.Windows.Forms.TextBox txtName;

         private System.Windows.Forms.Button btnGet;

         private System.Windows.Forms.Button btnExit;

         private System.Windows.Forms.TextBox txtCPUInfo;

         private System.Windows.Forms.Label label4;

         /// <summary>

         /// 必需的设计器变量。

         /// </summary>

         private System.ComponentModel.Container components = null;

 

         public Form1()

         {

              //

              // Windows 窗体设计器支持所必需的

              //

              InitializeComponent();

 

              //

              // TODO: 在 InitializeComponent 调用后添加任何构造函数代码

              //

         }

 

         /// <summary>

         /// 清理所有正在使用的资源。

         /// </summary>

         protected override void Dispose( bool disposing )

         {

              if( disposing )

              {

                   if (components != null)

                   {

                       components.Dispose();

                   }

              }

              base.Dispose( disposing );

         }

 

         #region Windows 窗体设计器生成的代码

         /// <summary>

         /// 设计器支持所需的方法 - 不要使用代码编辑器修改

         /// 此方法的内容。

         /// </summary>

         private void InitializeComponent()

         {

              this.label1 = new System.Windows.Forms.Label();

              this.label2 = new System.Windows.Forms.Label();

              this.label3 = new System.Windows.Forms.Label();

              this.txtMac = new System.Windows.Forms.TextBox();

              this.txtIP = new System.Windows.Forms.TextBox();

              this.txtName = new System.Windows.Forms.TextBox();

              this.btnGet = new System.Windows.Forms.Button();

              this.btnExit = new System.Windows.Forms.Button();

              this.txtCPUInfo = new System.Windows.Forms.TextBox();

              this.label4 = new System.Windows.Forms.Label();

              this.SuspendLayout();

              //

              // label1

              //

              this.label1.Location = new System.Drawing.Point(24, 35);

              this.label1.Name = "label1";

              this.label1.Size = new System.Drawing.Size(80, 16);

              this.label1.TabIndex = 0;

              this.label1.Text = "本机MAC地址:";

              //

              // label2

              //

              this.label2.Location = new System.Drawing.Point(24, 72);

              this.label2.Name = "label2";

              this.label2.Size = new System.Drawing.Size(80, 16);

              this.label2.TabIndex = 1;

              this.label2.Text = "本机IP地址:";

              //

              // label3

              //

              this.label3.Location = new System.Drawing.Point(24, 112);

              this.label3.Name = "label3";

              this.label3.Size = new System.Drawing.Size(80, 16);

              this.label3.TabIndex = 2;

              this.label3.Text = "计算机名:";

              //

              // txtMac

              //

              this.txtMac.Location = new System.Drawing.Point(120, 32);

              this.txtMac.Name = "txtMac";

              this.txtMac.Size = new System.Drawing.Size(160, 21);

              this.txtMac.TabIndex = 3;

              this.txtMac.Text = "";

              //

              // txtIP

              //

              this.txtIP.Location = new System.Drawing.Point(120, 69);

              this.txtIP.Name = "txtIP";

              this.txtIP.Size = new System.Drawing.Size(160, 21);

              this.txtIP.TabIndex = 4;

              this.txtIP.Text = "";

              //

              // txtName

              //

              this.txtName.Location = new System.Drawing.Point(120, 106);

              this.txtName.Name = "txtName";

              this.txtName.Size = new System.Drawing.Size(160, 21);

              this.txtName.TabIndex = 5;

              this.txtName.Text = "";

              //

              // btnGet

              //

              this.btnGet.Location = new System.Drawing.Point(80, 176);

              this.btnGet.Name = "btnGet";

              this.btnGet.TabIndex = 6;

              this.btnGet.Text = "点击获取";

              this.btnGet.Click += new System.EventHandler(this.btnGet_Click);

              //

              // btnExit

              //

              this.btnExit.Location = new System.Drawing.Point(176, 176);

              this.btnExit.Name = "btnExit";

              this.btnExit.TabIndex = 7;

              this.btnExit.Text = "退出";

              this.btnExit.Click += new System.EventHandler(this.btnExit_Click);

              //

              // txtCPUInfo

              //

              this.txtCPUInfo.Location = new System.Drawing.Point(120, 136);

              this.txtCPUInfo.Name = "txtCPUInfo";

              this.txtCPUInfo.Size = new System.Drawing.Size(160, 21);

              this.txtCPUInfo.TabIndex = 9;

              this.txtCPUInfo.Text = "";

              //

              // label4

              //

              this.label4.Location = new System.Drawing.Point(24, 144);

              this.label4.Name = "label4";

              this.label4.Size = new System.Drawing.Size(80, 16);

              this.label4.TabIndex = 8;

              this.label4.Text = "CPU序列号:";

              //

              // Form1

              //

              this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

              this.ClientSize = new System.Drawing.Size(312, 254);

              this.Controls.Add(this.txtCPUInfo);

              this.Controls.Add(this.label4);

              this.Controls.Add(this.btnExit);

              this.Controls.Add(this.btnGet);

              this.Controls.Add(this.txtName);

              this.Controls.Add(this.txtIP);

              this.Controls.Add(this.txtMac);

              this.Controls.Add(this.label3);

              this.Controls.Add(this.label2);

              this.Controls.Add(this.label1);

              this.Name = "Form1";

              this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;

              this.Text = "获取计算机名,IP地址,MAC地址";

              this.ResumeLayout(false);

 

         }

         #endregion

 

         /// <summary>

         /// 应用程序的主入口点。

         /// </summary>

         [STAThread]

         static void Main()

         {

              Application.Run(new Form1());

         }

 

         private void btnGet_Click(object sender, System.EventArgs e)

         {

              string IPAddress="",MACAddress="";           

             

              //计算机名computer name      

              string hostInfo = Dns.GetHostName();     

               

              //IP地址

              System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;          

              for (int i = 0; i < addressList.Length; i ++)     

              {      

                   IPAddress += addressList[i].ToString();     

              }            

             

              //mac地址  

             

              ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");    

              ManagementObjectCollection moc=mc.GetInstances();    

              foreach(ManagementObject mo in moc)  

              {      

                   if(mo["IPEnabled"].ToString()=="True")   

                   {

                       MACAddress += mo["MacAddress"].ToString();

                   }

              }  

        

              //cpu序列号

 

              string cpuInfo = "";

              ManagementClass cimobject = new ManagementClass("Win32_Processor");

              ManagementObjectCollection moco = cimobject.GetInstances();

              foreach(ManagementObject mo in moco)

              {

                   cpuInfo  += mo.Properties["ProcessorId"].Value.ToString();

              } 

 

              txtCPUInfo.Text = cpuInfo;

              txtName.Text = hostInfo;   

              txtIP.Text = IPAddress;        

              txtMac.Text = MACAddress;      

              btnGet.Enabled = false;   

              btnExit.Focus();

         }

 

         private void btnExit_Click(object sender, System.EventArgs e)

         {

              Application.Exit();

         }

     }

}

原创粉丝点击