• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

c#、Asp.net获取本地IP和MAC地址

原作者: [db:作者] 来自: [db:来源] 收藏 邀请
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();  
        }  
    }  

 第二种方式

         
        /// <summary>
        /// 获取Mac地址
        /// </summary>
        /// <param name="DestIP"></param>
        /// <param name="SrcIP"></param>
        /// <param name="pMacAddr"></param>
        /// <param name="PhyAddrLen"></param>
        /// <returns></returns>
        [DllImport("Iphlpapi.dll")]
        public static extern uint SendARP(uint DestIP, uint SrcIP, ref ulong pMacAddr, ref uint PhyAddrLen);
        public static string GetMac()
        {
            string p_Id = GetInternalIP();

            IPAddress _Address;
            if (!IPAddress.TryParse(p_Id, out _Address)) return "";
            uint DestIP = System.BitConverter.ToUInt32(_Address.GetAddressBytes(), 0);
            ulong pMacAddr = 0;
            uint PhyAddrLen = 6;
            uint error_code = SendARP(DestIP, 0, ref pMacAddr, ref PhyAddrLen);
            byte[] _Bytes1 = BitConverter.GetBytes(pMacAddr);
            return BitConverter.ToString(_Bytes1, 0, 6);
        }
        //获取内网IP
        protected static string GetInternalIP()
        {
            IPHostEntry host;
            string localIP = "?";
            host = Dns.GetHostEntry(Dns.GetHostName());
            foreach (IPAddress ip in host.AddressList)
            {
                if (ip.AddressFamily.ToString() == "InterNetwork")
                {
                    localIP = ip.ToString();
                    break;
                }
            }
            return localIP;
        }

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
php优势-PHP与ASP.NET的比较(转)发布时间:2022-07-10
下一篇:
Understanding ASP.NET MVC Filters and Attributes发布时间:2022-07-10
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap