用C#获得计算机名称,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();  

 

           }  

 

       }  

 

}  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值