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

相关推荐
百事牛科技7 小时前
从局部到整体:Excel限制功能的几种实用设置
windows·excel
2601_949695598 小时前
3DM游戏运行库合集安装教程,缺少DLL文件这样修复
windows·游戏·电脑
半摆烂日常8 小时前
手机表单的左滑操作:触摸事件的处理细节
大数据·开发语言·人工智能·智能手机·https·c#
绛橘色的日落(。・∀・)ノ8 小时前
C#学习笔记
笔记·学习·c#
asdzx678 小时前
C# 将 HTML 转换为 PDF:基于 Chrome 的转换方案
pdf·c#·html
Nontee9 小时前
Quartz 新手入门:定时任务的两个承诺
开发语言·windows·python
1-1=09 小时前
.NET 实现后台任务
c#·.net·.netcore·quartz·hangfire
weixin_4379619210 小时前
Docker安装(windows)
windows·docker·容器
步步为营DotNet10 小时前
从分代预算到 DATAS:.NET GC 调优到底在调什么
c#·.net·内存管理·gc·datas
淡海水10 小时前
05-03-源码-Boehm GC分配与碎片管理
开发语言·算法·unity·c#·游戏引擎·gc