I'm having an issue where my code is setting Static IP and DHCP perfectly fine when I'm connected to the WiFi. However, the Ethernet "local networkinterface" is unchangable, when no cable is connected. The issue is vice-versa for when connected with cable, and trying to set the WiFi interface.
The issue seem to only be present when the interface is disconnected and have no live connection. Anyone else able to reproduce? innput nicName is the name of interface I'm sending from my Form, and is basically used for matching it to the interfaceI want to change
Set StaticIP:
public static bool SetStaticIp(
string nicName,
Object objSite )
{
Site site = (Site)objSite;
//string nicName = adapter.Description;
string ipAddress = $"{site.IP}";
string subnetMask = $"{site.SubnetMask}";
string gateway = $"{site.Gateway}";
string dns1 = $"{site.Dns1}";
string dns2 = $"{site.Dns2}";
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
NetworkInterface networkInterface = interfaces.FirstOrDefault(x => x.Name == nicName);
string nicDesc = nicName;
if (networkInterface != null)
{
nicDesc = networkInterface.Description;
}
foreach (ManagementObject mo in moc)
{
bool moIpEnable = (bool)mo["IPEnabled"];
string moDesc = (string)mo["Description"];
if (moDesc.Equals(nicName) == true)
{
try
{
ManagementBaseObject newIP = mo.GetMethodParameters("EnableStatic");
newIP["IPAddress"] = new string[] { ipAddress };
newIP["SubnetMask"] = new string[] { subnetMask };
ManagementBaseObject setIP = mo.InvokeMethod("EnableStatic", newIP, null);
if (gateway != null)
{
ManagementBaseObject newGateway = mo.GetMethodParameters("SetGateways");
newGateway["DefaultIPGateway"] = new string[] { gateway };
newGateway["GatewayCostMetric"] = new int[] { 1 };
ManagementBaseObject setGateway = mo.InvokeMethod("SetGateways", newGateway, null);
}
if (dns1 != null || dns2 != null)
{
ManagementBaseObject newDns = mo.GetMethodParameters("SetDNSServerSearchOrder");
var dns = new List<string>();
if (dns1 != null)
{
dns.Add(dns1);
}
if (dns2 != null)
{
dns.Add(dns2);
}
newDns["DNSServerSearchOrder"] = dns.ToArray();
ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDns, null);
}
}
catch (Exception err)
{
Log(LOG_FN.ERROR, $"SetStaticIp({nicDesc}): Klarte ikke sette statisk IP. {err.Message}");
return false;
}
}
}
return true;
}
Set DHCP
public static int SetDHCP(string nicName)
{
//string nicName = sroGlobal.SelectedAdapters.Description;
//string nicName = Name;
ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection moc = mc.GetInstances();
NetworkInterface[] interfaces = NetworkInterface.GetAllNetworkInterfaces();
NetworkInterface networkInterface = interfaces.FirstOrDefault(x => x.Name == nicName);
ManagementObjectCollection networkCollection = mc.GetInstances();
string nicDesc = nicName;
if (networkInterface != null)
{
nicDesc = networkInterface.Description;
}
foreach (ManagementObject mo in networkCollection)
{
bool moIpEnable = (bool)mo["IPEnabled"];
string moDesc = (string)mo["Description"];
if (string.Compare(moDesc, nicName,StringComparison.InvariantCultureIgnoreCase) == 0)
{
try
{
ManagementBaseObject newDNS = mo.GetMethodParameters("SetDNSServerSearchOrder");
newDNS["DNSServerSearchOrder"] = null;
ManagementBaseObject enableDHCP = mo.InvokeMethod("EnableDHCP", null, null);
ManagementBaseObject setDNS = mo.InvokeMethod("SetDNSServerSearchOrder", newDNS, null);
return 1;
}
catch (Exception err)
{
Log(LOG_FN.ERROR, $"SetDHCP({nicDesc}): Could'nt set DHCP. {err.Message}");
return -1;
}
}
}
return 0;
}
edit 1: format error