I want to set an static ip (IPv4) to my LAN adapter settings. Former state: LAN adapter "XY" settings set to auto ip configuration Wanted state: LAN adapter "XY" settings set to an static ip and subnet mask
With the following code I can change my ip and subnet mask only when static ip is activated, but I want to change it also when auto ip config is activated. I don't know how to modify 'if (!(bool)objMO["IPEnabled"])' so that I can choose the LAN adapter "XY". Currently the LAN adapter with static ip is chosen for ip change.
ManagementClass objMC = new ManagementClass(
"Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if (!(bool)objMO["IPEnabled"])
continue;
try
{
ManagementBaseObject objNewIP = null;
ManagementBaseObject objSetIP = null;
objNewIP = objMO.GetMethodParameters("EnableStatic");
//Set IPAddress and Subnet Mask
objNewIP["IPAddress"] = new string[] { IPAddress };
objNewIP["SubnetMask"] = new string[] { SubnetMask };
objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, null);
}
}
Nearly I have the same question as (here), but I want to change the network configuration of a fixed LAN-adapter name.