- using System;
- using System.Management;
- using System.Net;
- public class Program
- {
- static void Main(string[] args)
- {
- try
- {
- string ip = "";
- string mac = "";
- ManagementClass mc;
- string hostInfo = Dns.GetHostName();
- //IP地址
- //System.Net.IPAddress[] addressList = Dns.GetHostByName(Dns.GetHostName()).AddressList;这个过时
- System.Net.IPAddress[] addressList = Dns.GetHostEntry(Dns.GetHostName()).AddressList;
- for (int i = 0; i < addressList.Length; i++)
- {
- ip = addressList[i].ToString();
- }
- //mac地址
- mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
- ManagementObjectCollection moc = mc.GetInstances();
- foreach (ManagementObject mo in moc)
- {
- if (mo["IPEnabled"].ToString() == "True")
- {
- mac = mo["MacAddress"].ToString();
- }
- }
- //输出
- string outPutStr = "IP:{0},\\n MAC地址:{1}";
- outPutStr = string.Format(outPutStr, ip, mac);
- Console.WriteLine(outPutStr);
- }
- catch (Exception e)
- { }
- Console.ReadLine();
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/270620134320.html
来源: http://www.codesnippet.cn/detail/270620134320.html