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

相关推荐
csdn_aspnet32 分钟前
C# 结合 JavaScript 实现手写板签名并上传到服务器
javascript·c#
回吐泡泡oO36 分钟前
Windows系统服务器蓝屏解决方案
windows
我不是程序猿儿41 分钟前
【C#】软件设计,华为的IPD学习之需求开发心得
学习·华为·c#
派大鑫wink41 分钟前
【Day13】集合框架(一):List 接口(ArrayList vs LinkedList)实战
java·开发语言·windows
love530love1 小时前
让 ComfyUI 官方 CLI 在 Windows CMD 里也能 Tab 补全 —— 实测与避坑记录
人工智能·windows·python·clink·comfy-cli·命令补全·clickcompletion
Rabi'1 小时前
Windows系统 Qt 整合 OpenCV4.12.0
开发语言·windows·qt·opencv
友莘居士1 小时前
Windows下Node.js 执行Web3.js 的智能合约环境搭建
windows·node.js·web3
_codemonster1 小时前
python易混淆知识点(十五)迭代器
开发语言·windows·python
WebRuntime1 小时前
所有64位WinForm应用都是Chromium浏览器
javascript·c++·c#·.net·web
zhlx28351 小时前
免费开源跨平台听歌自由!自定义音乐源 + 桌面歌词!LX Music 落雪音乐开源版
android·windows·macos