'Get Parent property of network adapter device from c#

I need to get the Parent property of network adapter device from c# program

Please find the image here

Tried below code but the property "Parent" is not available

List mappingIpDeviceIds = new List();

        Console.WriteLine("Reading Id and IP address...");
        ManagementObjectSearcher adapters = new ManagementObjectSearcher("SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionStatus = 2");

        foreach (ManagementObject item in adapters.Get())
        {
            foreach (ManagementObject setting in item.GetRelated("Win32_NetworkAdapterConfiguration"))
            {
                var relationships = setting.GetRelationships();
                var name = setting["Caption"].ToString();

                
                string[] defaultIPGateway = (string[])setting.GetPropertyValue("DefaultIPGateway");
                string[] compterips = (string[])setting.GetPropertyValue("IPAddress");
                string dhcpserver = (string)setting.GetPropertyValue("DHCPServer");

                foreach (ManagementObject win32PnPEntity in item.GetRelated("Win32_PnPEntity"))
                {
                    var x = win32PnPEntity.Properties;

                    foreach (var prop in x)
                    {
                        Console.WriteLine("::: PROPERTY NAME ::: " + prop.Name);
                        Console.WriteLine("::: PROPERTY VALUE ::: " + prop.Value);
                    }
                }

                MappingIpDeviceId mappingIpDeviceId = new MappingIpDeviceId();
                string deviceid = (string)item.GetPropertyValue("PNPDeviceID");
                string deviceid1 = (string)item.GetPropertyValue("ProductName");

                string[] devicenames = deviceid.Split(new Char[] { '\\' });
                foreach (string dn in devicenames)
                {
                    mappingIpDeviceId.OWLIP = dhcpserver;
                    mappingIpDeviceId.ComputerIP = compterips[0];
                    mappingIpDeviceId.DeviceId = dn;
                    mappingIpDeviceIds.Add(mappingIpDeviceId);
                }
                
            }
        }


Solution 1:[1]

There are two ways you can refer to:

  1. Using PowerShell command Get-PnpDeviceProperty: Get-PnpDeviceProperty -KeyName 'DEVPKEY_Device_Parent' -InstanceId 'xxxxxxxxxxxxxxxx'
  2. Use CM_Get_Parent. "Determining the Parent of a Device".

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rita Han