using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Collections;
using System.Net;
using System.Management;
namespace 获取本机名_IP地址_MAC地址
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string s = "", mac = "";
string hostinfo = Dns.GetHostName();
IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;
for (int i = 0; i < addressList.Length;i++ )
{
s += addressList[i].ToString();
}
ManagementClass mc;
mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
foreach(ManagementObject mo in moc)
{
if (mo["IPEnabled"].ToString() == "True")
{
mac = mo["MacAddress"].ToString();
}
}
txtName.Text = hostinfo;//本机名
txtIP.Text = s;//IP地址
txtMAC.Text = mac;//MAC地址
}
}
}