码迷,mamicode.com
首页 > Windows程序 > 详细

C# 获取MAC地址

时间:2016-12-09 16:35:52      阅读:206      评论:0      收藏:0      [点我收藏+]

标签:.com   arp   文档   overflow   space   nic   mac地址   stat   collect   

/**********************************************************************
 *                         C# 获取MAC地址
 * 说明:
 *     在C#中获取本机的MAC地址,文中提供两个参考,一个是能够所有的MAC
 * 地址,一个是获取第一个MAC地址。
 *
 *                                  2016-12-9 深圳 南山平山村 曾剑锋
 *********************************************************************/

一、参考文档:
    1. Reliable method to get machines MAC address in C#
        http://stackoverflow.com/questions/850650/reliable-method-to-get-machines-mac-address-in-c-sharp

二、解决方法:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.NetworkInformation; 

    namespace LocalDetectTest
    {
        class NetTools
        {
            /// <summary>
            /// Finds the MAC address of the NIC with maximum speed.
            /// </summary>
            /// <returns>The MAC address.</returns>
            public static void PrintAllMacAddress()
            {
                const int MIN_MAC_ADDR_LENGTH = 12;
                string macAddress = string.Empty;
                long maxSpeed = -1;

                foreach (NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces())
                {
                    Console.WriteLine(
                        "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)
                    {
                        Console.WriteLine("New Max Speed = " + nic.Speed + ", MAC: " + tempMac);
                        maxSpeed = nic.Speed;
                        macAddress = tempMac;
                    }
                }

                // return macAddress;
            }

            /// <summary>
            /// Finds the MAC address of the first operation NIC found.
            /// </summary>
            /// <returns>The MAC address.</returns>
            public static string GetFirstMacAddress()
            {
                string macAddresses = string.Empty;

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

                return macAddresses;
            }
        }
    }

 

C# 获取MAC地址

标签:.com   arp   文档   overflow   space   nic   mac地址   stat   collect   

原文地址:http://www.cnblogs.com/zengjfgit/p/6149713.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!