The following query returns two Win32_NetworkAdapterConfiguration
objects
on my Windows 8 machine:
ManagementObjectSearcher query =
new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE DHCPEnabled ='TRUE'");
ManagementObjectCollection queryCollection = query.Get();
queryCollection = query.Get();
foreach (ManagementObject currentConfig in queryCollection)
{
string[] addresses = (string[])currentConfig["IPAddress"];
Console.Out.WriteLine(currentConfig["Description"]);
if (addresses != null)
{
foreach (var addr in addresses)
{
Console.Out.WriteLine(addr);
}
}
}
The first adapter configuration is for the Microsoft Kernel Debug Network Adapter.
This adapter is used for remote kernel debugging. The DHCPEnabled
property
for this adapter is TRUE
. However there are no IP addresses
assigned to this adapter. This is the reason why the IPAddress
array is null.
The second adapter configuration is for my Intel Gigabit Network Adapter. There are two IP addresses assigned to the adapter.
Another reason why the IPAddress
array could be null is
for example a disconnected wireless network adapter.
So, in order to solve your NullReferenceException
you have to check the
IPAddress
array for null (please see the code above). Use the Description
property to identify the adapter configuration.