• 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地址

  • 相关阅读:
    sql日期操作
    用户活跃状态模型
    R基于Bayes理论实现中文人员特性的性别判定
    python的一点小常识
    [工作技能]SVN
    北大计算所词性标注集简表
    c++ boost 汉字和模式串混用的例子
    SVN文本文件报二进制属性的问题
    我的R代码备份
    FaceBook Twitter实习生简历求内推
  • 原文地址:https://www.cnblogs.com/sc2015/p/5532029.html
Copyright © 2020-2023  润新知