【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.调试结果

相关推荐
子燕若水42 分钟前
Nuitka 打包 教程windows版本
windows
吴声子夜歌2 小时前
Windows——注册表
windows
i***11863 小时前
Windows环境下安装Redis并设置Redis开机自启
数据库·windows·redis
m5655bj4 小时前
通过 C# 将 RTF 文档转换为图片
开发语言·c#
5***g2985 小时前
Windows安装Rust环境(详细教程)
开发语言·windows·rust
吴声子夜歌5 小时前
Windows——PowerShell
windows
MM_MS5 小时前
WinForm+C#小案例--->写一个记事本程序
开发语言·计算机视觉·c#·visual studio
极简之美6 小时前
Mac 远程连接 Windows 简明教程(2025 实测版)
windows·macos
电脑小管家7 小时前
蝰蛇鼠标驱动怎么安装?全型号驱动下载方法汇总
windows·驱动开发·计算机外设·电脑·游戏程序
无限进步_7 小时前
C++从入门到类和对象完全指南
开发语言·c++·windows·git·后端·github·visual studio