如何使用C#.net获取唯一的以太网MACC地址 [英] how to get the only ethernet macc address using C#.net

查看:158
本文介绍了如何使用C#.net获取唯一的以太网MACC地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi

这是我用于获取macc地址的代码.
它将返回适配器的maccaddress,表示已启用其中一个
所以我只需要用于maccaddress的以太网适配器


public string GetMACAddress()
       {
           ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
           ManagementObjectCollection moc = mc.GetInstances();
           string MACAddress = String.Empty;
           foreach (ManagementObject mo in moc)
           {
               if (MACAddress == String.Empty) // only return MAC Address from first card
               {
                   if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
               }
               mo.Dispose();
           }
           MACAddress = MACAddress.Replace(":", "");
           return MACAddress;

       }




请指定hint

解决方案

我不确定问题出在哪里.

您的代码将获取第一个启用的网络适配器的MAC地址,并去除分号.这不是您所期望的吗?

干杯.


如果当前未启用NIC卡,则无法通过WMI看到它.


您可以您认为Windows联网的工作方式存在问题.

一台计算机可以一次启用多个以太网适配器,并且它们都将拥有自己的MAC地址.机器没有一个" MAC地址.

另外,如果出于某种安全原因尝试使用MAC地址,请不要使用.您获得的第一个" MAC地址可能与您之前获得的第一个" MAC地址不同.无法保证适配器的配置顺序,不能保证仍启用相同的适配器,某些适配器(通常是笔记本电脑)将允许您通过设备属性页面更改MAC地址.


hi

this is code i am using for getting the macc address.
it will returnes the maccaddress of adapter ,means which one is enabled
so i need only the ethernet adapter for maccaddress


public string GetMACAddress()
       {
           ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
           ManagementObjectCollection moc = mc.GetInstances();
           string MACAddress = String.Empty;
           foreach (ManagementObject mo in moc)
           {
               if (MACAddress == String.Empty) // only return MAC Address from first card
               {
                   if ((bool)mo["IPEnabled"] == true) MACAddress = mo["MacAddress"].ToString();
               }
               mo.Dispose();
           }
           MACAddress = MACAddress.Replace(":", "");
           return MACAddress;

       }




please specify that hint

解决方案

I''m not sure what the problem is.

Your code fetches the MAC address of the first enabled network adapter and strips out the semicolons. Is this not what you are expecting?

Cheers.


If the NIC card isn''t currently enabled, you won''t be able to see it with WMI.


You may have a problem with the way you think Windows networking works.

A machine can have multiple Ethernet adapters enabled all at once and they will all have their own MAC addresses. There is no "one" MAC address for the machine.

Also, if you''re trying to use the MAC address for some security reason, don''t. The "first" MAC you get back may not be the same "first" MAC address you got before. There is no guarantee of adapter configuration order, no guarantee that the same adapters will still be enabled, and some adapters (mostly laptop) will let you change the MAC address through the devices property page.


这篇关于如何使用C#.net获取唯一的以太网MACC地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆