欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)獲取MAC地址的方法

 更新時(shí)間:2014年08月07日 10:30:36   投稿:shichen2014  
這篇文章主要介紹了C#實(shí)現(xiàn)獲取MAC地址的方法,很實(shí)用的功能,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)獲取MAC地址的方法,是一個(gè)非常常見(jiàn)而且實(shí)用的功能,具體方法如下:

主要功能代碼如下:

/// <summary>
/// 根據(jù)網(wǎng)卡類(lèi)型來(lái)獲取mac地址
/// </summary>
/// <param name="networkType">網(wǎng)卡類(lèi)型</param>
/// <param name="macAddressFormatHanlder">格式化獲取到的mac地址</param>
/// <returns>獲取到的mac地址</returns>
public static string GetMacAddress(NetworkInterfaceType networkType, Func<string, string> macAddressFormatHanlder)
{
  string _mac = string.Empty;
  NetworkInterface[] _networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
  foreach (NetworkInterface adapter in _networkInterfaces)
  {
 if (adapter.NetworkInterfaceType == networkType)
 {
   _mac = adapter.GetPhysicalAddress().ToString();
   if (!String.IsNullOrEmpty(_mac))
 break;
 }
  }
  if (macAddressFormatHanlder != null)
 _mac = macAddressFormatHanlder(_mac);
  return _mac;
}
/// <summary>
/// 根據(jù)網(wǎng)卡類(lèi)型以及網(wǎng)卡狀態(tài)獲取mac地址
/// </summary>
/// <param name="networkType">網(wǎng)卡類(lèi)型</param>
/// <param name="status">網(wǎng)卡狀態(tài)</param>
///Up 網(wǎng)絡(luò)接口已運(yùn)行,可以傳輸數(shù)據(jù)包。 
///Down 網(wǎng)絡(luò)接口無(wú)法傳輸數(shù)據(jù)包。 
///Testing 網(wǎng)絡(luò)接口正在運(yùn)行測(cè)試。 
///Unknown 網(wǎng)絡(luò)接口的狀態(tài)未知。 
///Dormant 網(wǎng)絡(luò)接口不處于傳輸數(shù)據(jù)包的狀態(tài);它正等待外部事件。 
///NotPresent 由于缺少組件(通常為硬件組件),網(wǎng)絡(luò)接口無(wú)法傳輸數(shù)據(jù)包。 
///LowerLayerDown 網(wǎng)絡(luò)接口無(wú)法傳輸數(shù)據(jù)包,因?yàn)樗\(yùn)行在一個(gè)或多個(gè)其他接口之上,而這些“低層”接口中至少有一個(gè)已關(guān)閉。 
/// <param name="macAddressFormatHanlder">格式化獲取到的mac地址</param>
/// <returns>獲取到的mac地址</returns>
public static string GetMacAddress(NetworkInterfaceType networkType, OperationalStatus status, Func<string, string> macAddressFormatHanlder)
{
  string _mac = string.Empty;
  NetworkInterface[] _networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
  foreach (NetworkInterface adapter in _networkInterfaces)
  {
 if (adapter.NetworkInterfaceType == networkType)
 {
   if (adapter.OperationalStatus != status) continue;
   _mac = adapter.GetPhysicalAddress().ToString();
   if (!String.IsNullOrEmpty(_mac)) break;
 }
  }
  if (macAddressFormatHanlder != null)
 _mac = macAddressFormatHanlder(_mac);
  return _mac;
}
/// <summary>
/// 獲取讀到的第一個(gè)mac地址
/// </summary>
/// <returns>獲取到的mac地址</returns>
public static string GetMacAddress(Func<string, string> macAddressFormatHanlder)
{
  string _mac = string.Empty;
  NetworkInterface[] _networkInterfaces = NetworkInterface.GetAllNetworkInterfaces();
  foreach (NetworkInterface adapter in _networkInterfaces)
  {
 _mac = adapter.GetPhysicalAddress().ToString();
 if (!string.IsNullOrEmpty(_mac))
   break;
  }
  if (macAddressFormatHanlder != null)
 _mac = macAddressFormatHanlder(_mac);
  return _mac;
}

有些項(xiàng)目中出于安全考慮需要獲取MAC地址,然后再判斷MAC地址是否合法才可以登陸。本文總結(jié)的方法希望對(duì)大家有所幫助!

相關(guān)文章

最新評(píng)論