获取mac地址

来源:互联网 发布:python bloomfilter 编辑:程序博客网 时间:2024/06/08 08:39
    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;
    }


原创粉丝点击