public void SetStatic()
{
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc;
try
{
moc = mc.GetInstances();
foreach (ManagementObject mo in moc)
{
if ((bool)mo["ipEnabled"]) //is adapter enabled
{
if (mo["Caption"].Equals(Name)) //is this the adapter i am looking for
{
ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic"); //Enable static ip on the adapter
ManagementBaseObject newGateway = mo.GetMethodParameters("SetGateways"); //Enable custom Gateway addreses
ManagementBaseObject newDns = mo.GetMethodParameters("SetDNSServerSearchOrder");
newGateway["DefaultIPGateway"] = gateways.ToArray(); //Set the gateways
newGateway["GatewayCostMetric"] = new int[] { 1 }; //Set metric cost to 1
newIP["IPAddress"] = ipAddresses.ToArray(); //Set the ip address of the adapter
newIP["SubnetMask"] = subnets.ToArray(); //Set the subnets
newDns["DNSServerSearchOrder"] = dnses.ToArray(); //Set the dnses
//the following three statement do the actual work of telling the addaper what goes where
ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null);
ManagementBaseObject setGateways = mo.InvokeMethod("SetGateways", newGateway, null);
ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDns, null);
break;
}//End if - is this the addapter i am looking for
}//End if - is adapter enabled
}//End foreach - loop through management objects
}
catch (Exception err)
{
throw new ApplicationException(err.Message + "\n" + err.StackTrace);
}//End catch
}//End SetStatic