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

相关推荐
想拿大厂offer4 小时前
Windows Server 2022 + IIS + .NET 8 + MySQL 部署手册
c#·asp.net
liuhuapeng03044 小时前
GetMapping自动截取List<String>字符
数据结构·windows·list
小码编匠6 小时前
.NET 10 性能突破:持续优化才是质变关键
后端·c#·.net
魏 无羡6 小时前
windows 安装mysql(多个版本同时安装)
windows·mysql·adb
mudtools9 小时前
.NET驾驭Excel之力:Excel应用程序的创建与管理
c#·.net·excel·wps
mudtools9 小时前
.NET驾驭Excel之力:自动化数据处理 - 开篇概述与环境准备
c#·自动化·.net·excel·wps
唐青枫9 小时前
C#.NET WebAPI 返回类型深度解析:IActionResult 与 ActionResult<T> 的区别与应用
c#·.net
芳草萋萋鹦鹉洲哦14 小时前
【Windows】tauri+rust运行打包工具链安装
开发语言·windows·rust
会跑的兔子15 小时前
Android 16 Kotlin协程 第二部分
android·windows·kotlin
hhhh明17 小时前
quest2+alvr+steamvr
linux·windows·quest2