问题
I want to accesss the mac address of the computer using c#. I have used the following code to access mac address but there is some issue in this code.
Code 1
foreach( NetworkInterface nic in NetworkInterface.GetAllNetworkInterfaces() )
{
if( nic.OperationalStatus == OperationalStatus.Up )
{
Console.WriteLine( nic.GetPhysicalAddress().ToString() );
checkMAC = nic.GetPhysicalAddress().ToString();
break;
}
}
Code 2
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(":", "");
In first code when we disconnected from network connection then it will return null mac address . second code return the mac address when network adapter connection id disconnected But when we disbled the network connection or remove the IP address of the computer then it will return the null mac address .
How to get the mac address when network connnection disabled, there is no IP address assigned to the PC or network connection disconnected?
回答1:
When you disable your network adapter, you can't access it at all - it is as if it isn't installed, which is why you don't see a MAC address.
EDIT: Explanation:
A MAC address belongs to a network adapter. If you have 3 adapters you have 3 MAC addresses. If you have no adapters, you have no MAC address.
来源:https://stackoverflow.com/questions/9544018/get-mac-address-of-computer