首页 > 编程 > C# > 正文

C#获取局域网MAC地址的简单实例

2020-01-24 03:03:02
字体:
来源:转载
供稿:网友

效果:

说明:获取本局域网的MAC地址(非本机的MAC地址)

代码:

复制代码 代码如下:

/// <summary>
        /// 获取网卡物理地址
        /// </summary>
        /// <returns></returns>
        public static string getMAC()
        {
            string madAddr = null;
            ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
            ManagementObjectCollection moc2 = mc.GetInstances();
            foreach (ManagementObject mo in moc2)
            {
                if (Convert.ToBoolean(mo["IPEnabled"]) == true)
                {
                    madAddr = mo["MacAddress"].ToString();
                    madAddr = madAddr.Replace(':', '-');
                }
                mo.Dispose();
            }
            return madAddr;
        }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表