C# 获取MAC地址
生活随笔
收集整理的這篇文章主要介紹了
C# 获取MAC地址
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
/*********************************************************************** C# 獲取MAC地址* 說明:* 在C#中獲取本機(jī)的MAC地址,文中提供兩個(gè)參考,一個(gè)是能夠所有的MAC* 地址,一個(gè)是獲取第一個(gè)MAC地址。** 2016-12-9 深圳 南山平山村 曾劍鋒*********************************************************************/一、參考文檔:1. Reliable method to get machine's 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;}}}
?
總結(jié)
以上是生活随笔為你收集整理的C# 获取MAC地址的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 嵌入式开发之zynq——zynq开发环境
- 下一篇: java.util.regex.Patt