0

I'm refactoring some older code and want to make sure I understand the previous logic.

Here is the original code:

ManagementClass myManagementClass = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = myManagementClass.GetInstances();
foreach (ManagementObject obj in moc)
{
    if (MACAddress == string.Empty)
    {
        if ((bool)obj["IPEnabled"] == true)
        {
            MACAddress = obj["MacAddress"].ToString();
            //dispose of our object
            obj.Dispose();
            break;
        }
    }
}

Here's the documentation for the Win32_NetworkAdapterConfiguration class.

I'd like to use the NetworkInterface class as described in this question.

What is the semantic meaning of "IPEnabled" and is it the same as OperationalStatus.Up? If not, is there a way to query the "IPEnabled" property using the NetworkInterface (or some other) class?

Most of these examples return the first network interface it finds. Could there be more than one that is "up" or is there always only one?

Community
  • 1
  • 1
Jason
  • 8,400
  • 10
  • 56
  • 69

1 Answers1

1

They are not the same.

What you want is to scan through each intrface and see if it supports IPv4 or IPv6:

For Each tInterface As NetworkInterface in NetworkInterface.GetAllNetworkInterfaces()
  If (tInterface.Supports(NetworkInterfaceComponent.IPv4)) Or (tInterface.Supports(NetworkInterfaceComponent.IPv6)) Then
    '
    '  Do something with tInterface.GetPhysicalAddress()
    '
  End If
Next
Sam Axe
  • 33,313
  • 9
  • 55
  • 89