I'm working on a tool for work that needs to reset the PC IP address to a specific IP and subnet mask.
I've used the code below to try to change the IP (taken from this page: How can you change Network settings (IP Address, DNS, WINS, Host Name) with code in C#).
The problem is, this code doesn't do anything. The IP address of my computer's local connection doesn't change - it's still being automatically set via DHCP.
Help?
public void SetIP(string ip_address, string subnet_mask)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
try
{
ManagementBaseObject setIP;
ManagementBaseObject newIP =
objMO.GetMethodParameters("EnableStatic");
newIP["IPAddress"] = new string[] { ip_address };
newIP["SubnetMask"] = new string[] { subnet_mask };
setIP = objMO.InvokeMethod("EnableStatic", newIP, null);
}
catch (Exception)
{
throw;
}
}
}
}