유용한 정보

[C#] Get MAC address

DevReff 2025. 4. 16. 21:36
728x90
728x90
SMALL

/// <summary>
/// 맥주소를 가져오는 함수
/// </summary>
/// <param name="isAll">모든 MAC 주소를 구할 것인지의 여부</param>
/// <returns>MAC 주소 목록</returns>
static public List<KeyValuePair<string, string>> GetMacAddress(bool isAll = false)
{
try
{
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
if (nics != null && nics.Count() > 0)
{
List<KeyValuePair<string, string>> aMAC = new List<KeyValuePair<string, string>>(); /// KeyValuePair<MAC Address, Network Interface Type>

foreach (NetworkInterface adapter in nics)
{
PhysicalAddress addr = adapter.GetPhysicalAddress();
string sAddr = addr.ToString();
NetworkInterfaceType nit = adapter.NetworkInterfaceType;
string sNit = nit.ToString();
if (isAll == false)
{
KeyValuePair<string, string> item = new KeyValuePair<string, string>(sAddr, sNit);
if (sAddr.Length >= 12 && aMAC.Contains(item) == false)
aMAC.Add(item);
}
else
{
if (sAddr.Length < 1)
sAddr = "0000000000000000";
aMAC.Add(new KeyValuePair<string, string>(sAddr, sNit));
}
}

if (aMAC.Count < 1)
return null;

return aMAC;
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}

return null;
}

728x90
728x90
LIST

'유용한 정보' 카테고리의 다른 글

[C#] 관리자 권한으로 실행하게 만들기  (0) 2025.04.18
[C#] 레지스트리 읽기/쓰기  (0) 2025.04.17
[C#] Import Excel File To DataGridView  (0) 2025.04.16
[C#] Excel to PrintPreview  (0) 2025.04.16
[C#] Excel To Printer  (0) 2025.04.16