C#通过WMI读取MAC地址

发布时间:2020-05-31 15:16:29 作者:码如龙
来源:网络 阅读:1027

该方法依赖WMI的系统服务,该服务一般不会被关闭;但如果系统服务缺失或者出现问题,该方法无法取得MAC地址,需要重启Windows Management Instrumentation服务。

public static string GetMacAddress()
        {
            try
            {
                string active_mac = "";
                ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
                ManagementObjectCollection moc = mc.GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"] == true)
                        active_mac = mo["MacAddress"].ToString();
                    mo.Dispose();
                }
                string mac = active_mac.Substring(0, 2) + active_mac.Substring(3, 2) + active_mac.Substring(6, 2) + active_mac.Substring(9, 2) + active_mac.Substring(12, 2) + active_mac.Substring(15, 2);
                return mac;
            }
            catch (Exception ex)
            {
                return "Error Mac." + ex.Message;
            }
        }
推荐阅读:
  1. 如何通过IP获取MAC地址?如何通过mac来获取IP地址?
  2. wmi.WMI().Win32_ComputerSystem()

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mac地址 wmi

上一篇:C++实现迷宫问题

下一篇:C++实现插入排序

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》