Unity网络通讯(一)获取计算机的MAC地址

 

 1   string GetMac()
 2    {
 3         string mac = "";
 4         mac = GetMacAddressBySendARP();
 5         return mac;
 6     }
 7     [DllImport("Iphlpapi.dll")]
 8     static extern int SendARP(Int32 DestIP, Int32 SrcIP, ref Int64 MacAddr, ref Int32 PhyAddrLen);
 9     /// <summary>  
10     /// SendArp获取MAC地址  
11     /// </summary>  
12     /// <returns></returns>  
13     public string GetMacAddressBySendARP()
14     {
15         StringBuilder strReturn = new StringBuilder();
16         try
17         {
18             System.Net.IPHostEntry Tempaddr = (System.Net.IPHostEntry)Dns.GetHostByName(Dns.GetHostName());
19             System.Net.IPAddress[] TempAd = Tempaddr.AddressList;
20             Int32 remote = (int)TempAd[0].Address;
21             Int64 macinfo = new Int64();
22             Int32 length = 6;
23             SendARP(remote, 0, ref macinfo, ref length);
24             string temp = System.Convert.ToString(macinfo, 16).PadLeft(12, '0').ToUpper();
25             int x = 12;
26             for (int i = 0; i < 6; i++)
27             {
28                 if (i == 5) { strReturn.Append(temp.Substring(x - 2, 2)); }
29                 else { strReturn.Append(temp.Substring(x - 2, 2) + ":"); }
30                 x -= 2;
31             }
32             return strReturn.ToString();
33         }
34         catch
35         {
36             return "";
37         }
38     }  

以上代码可直接调用GetMac()函数获取电脑的Mac地址

转载于:https://www.cnblogs.com/sc2015/p/5532029.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值