【C#】获取电脑网卡MAC地址

1.获取电脑可用网卡MAC地址

csharp 复制代码
/// <summary>
/// 获取电脑MAC地址
/// </summary>
/// <returns></returns>
public static List<string> GetMacByWmi()
{
    string key = "SYSTEM\\CurrentControlSet\\Control\\Network\\{4D36E972-E325-11CE-BFC1-08002BE10318}\\";
    List<string> macList = new List<string>();
    try
    {
        NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
        foreach (NetworkInterface adapter in nics)
        {
            if (adapter.NetworkInterfaceType == NetworkInterfaceType.Ethernet && adapter.GetPhysicalAddress().ToString().Length != 0)
            {
                string fRegistryKey = key + adapter.Id + "\\Connection";
                RegistryKey rk = Registry.LocalMachine.OpenSubKey(fRegistryKey, false);
                if (rk != null)
                {
                    //string fPnpInstanceID = rk.GetValue("PnpInstanceID", "").ToString();
                    //if (fPnpInstanceID.Length > 3 && fPnpInstanceID.Substring(0, 3) == "PCI")
                    {
                        string macAddress = adapter.GetPhysicalAddress().ToString();
                        for (int i = 1; i < 6; i++)
                        {
                            macAddress = macAddress.Insert(3 * i - 1, "-");
                        }
                        macList.Add(macAddress);
                        //break;
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
    }
    return macList;
}

2.调试结果

相关推荐
czhc11400756636 小时前
7.29:树形;
c#
cm04Z9c917 小时前
C#摸鱼实录——IoC与DI案例详解
开发语言·c#
数据知道9 小时前
Windows 安全基线:组策略、UAC、Defender 深度配置
windows·安全·网络安全
大模型码小白12 小时前
在 Windows 下 Codex 安装、配置与使用详细指南
java·人工智能·windows
小小龙学IT12 小时前
C++ Placement New 与显式析构:手动对象生命周期管理的艺术
c++·windows·mfc
geovindu13 小时前
CSharp:Chain of Responsibility Pattern
开发语言·后端·设计模式·c#·.net·责任链模式·行为模式
luyun02020213 小时前
论坛里的小工具,吾爱出品
运维·服务器·windows
老王生涯14 小时前
rust开发环境配置-Windows & GNU
开发语言·windows·rust
nLif14 小时前
进程管道通讯-伪终端方式
c++·windows
颜x小15 小时前
[C#]泛型类与泛型方法
开发语言·c++·c#