I have code that is meant to change/set my gateway values. The program runs smoothly, but it doesn't appear to have changed anything when I look at my IPV4 settings. What am I missing?
public void setGateway(string gateway = "192.168.1.254", string myDesc = "Intel(R) Ethernet Connection (2) I219-V")
{
var adapterConfig = new ManagementClass("Win32_NetworkAdapterConfiguration");
var networkCollection = adapterConfig.GetInstances();
foreach(ManagementObject adapter in networkCollection)
{
string description = adapter["Description"] as string;
if(string.Compare(description, myDesc, StringComparison.InvariantCultureIgnoreCase) >= 1)
{
try
{
var newGateway = adapter.GetMethodParameters("SetGateways");
newGateway["DefaultIPGateway"] = new string[] { gateway };
newGateway["GatewayCostMetric"] = new int[] { 1 };
adapter.InvokeMethod("SetGateways", newGateway, null);
}
catch
{
throw;
}
}
}
}
Also I referred in this link How can you change Network settings (IP Address, DNS, WINS, Host Name) with code in C#, I'm a bit confused right now, I have a static ip address and netmask already set.