.net core 获取网卡地址

   ///

 
    /// 通过SendARP获取网卡Mac 
    /// 网络被禁用或未接入网络(如没插网线)时此方法失灵 
    ///
 
    /// 
    /// 
    public static string GetMacBySendARP(string remoteIP)
    {
        StringBuilder macAddress = new StringBuilder();
        try
        {
            Int32 remote = inet_addr(remoteIP);
            Int64 macInfo = new Int64(); Int32 length = 6;
            SendARP(remote, 0, ref macInfo, ref length);
            string temp = Convert.ToString(macInfo, 16).PadLeft(12, '0').ToUpper();
            int x = 12; for (int i = 0; i < 6; i++)
            {
                if (i == 5) { macAddress.Append(temp.Substring(x - 2, 2)); } else { macAddress.Append(temp.Substring(x - 2, 2) + "-"); }
                x -= 2;
            }
            return macAddress.ToString();
        }
        catch
        {
            return macAddress.ToString();
        }
    }
    [DllImport("Iphlpapi.dll")]
    private static extern int SendARP(Int32 dest, Int32 host, ref Int64 mac, ref Int32 length);
    [DllImport("Ws2_32.dll")]
    private static extern Int32 inet_addr(string ip);

 

       ///


        /// 获取客户端Ip
        ///

        ///
        public string GetClientIp()
        {
            return _httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
        }

你可能感兴趣的:(.net core 获取网卡地址)