查找了几个方法,经过调试修改,下面这个方法能很好的获取到本地的IP和MAC地址。可以用于这方面的功能实现。主要是要添加System.Management的引用。
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(); } }c#获取本地IP和MAC地址
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
上一篇:C#窗口拦截键盘事件
下一篇:ASP.NET显示农历时间

提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
前端如何获取MAC地址
获取硬件mac地址
c++ mac地址 -
[C++][linux]linux C++获取mac地址
Follow。
linux c++ macos #include sed -
C++ 硬件信息 获取网卡MAC地址
获取计算机网卡MAC地址的方法很多,这里采用的是命令行的形式获取,可以获取有线网卡与无
硬件 网卡 mac 命令行 #include -
JAVA获取本地MAC地址
JAVA获取本地MAC地址
java jvm servlet mac地址 物理地址 -
如何通过IP获取MAC地址?如何通过mac来获取IP地址?
MAC的地址也可以修改的哦!!!
windows IP 地址 -
C/C++ 获取函数地址
CC语言中没有类的概念,只有普通的函数。通过函数名就可以得到函数地址#include <std
成员函数 静态函数 成员变量