获取mac地址

    public static void PrintAllMacAddress()
    {
        const int MIN_MAC_ADDR_LENGTH = 12;
        string macAddress = string.Empty;
        long maxSpeed = -1;


        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            print(
                "Name: " + nic.Name +
                " Found MAC Address: " + nic.GetPhysicalAddress() +
                " Type: " + nic.NetworkInterfaceType);


            string tempMac = nic.GetPhysicalAddress().ToString();
            if (nic.Speed > maxSpeed &&
                !string.IsNullOrEmpty(tempMac) &&
                tempMac.Length >= MIN_MAC_ADDR_LENGTH)
            {
                print("New Max Speed = " + nic.Speed + ", MAC: " + tempMac);
                maxSpeed = nic.Speed;
                macAddress = tempMac;
            }

        }


    public static string GetFirstMacAddress()
    {
        string macAddresses = string.Empty;


        foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
        {
            if (nic.OperationalStatus == OperationalStatus.Up)
            {
                macAddresses += nic.GetPhysicalAddress().ToString();
                print(macAddresses);
                break;
            }
        }


        return macAddresses;
    }


博主设置当前文章不允许评论。

没有更多推荐了,返回首页