端くれプログラマの備忘録 C# [C#] 有効なネットワークアダプタのMACアドレス取得

[C#] 有効なネットワークアダプタのMACアドレス取得

using System;
using System.Collections.Generic;

using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;

List<PhysicalAddress> GetPhysicalAddresses()
{
    var macAddresses = new List<PhysicalAddress>();
    var interfaces = NetworkInterface.GetAllNetworkInterfaces();

    foreach (var adapter in interfaces)
    {
        if (adapter.OperationalStatus == OperationalStatus.Up)
        {
            //Console.WriteLine(adapter.Description);
            //Console.WriteLine(adapter.GetPhysicalAddress().ToString());

            macAddresses.Add(adapter.GetPhysicalAddress());
        }
    }
    return macAddresses;
}