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();
}
}
}