Hello all,
I am trying to change the IP-Adress, Subnet and the Gateway of only one NIC, using a c#-Programm.
By searching the web I found one solution for changing all NICs of the client, but I need only to change the IP-Adress of my LAN adapter (not the wireless!).
Does somebody know how i can update only a specified LAN-Adapter?
Currently I have this code:
Code:
public void setIP(string ip_address, string subnet_mask, string nicName)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();
foreach (ManagementObject objMO in objMOC)
{
if ((bool)objMO["IPEnabled"])
{
try
{
if (objMO["Caption"].Equals(nicName)) //This is not working
{
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);
return;
}
}
catch (Exception)
{}
}
}
}
here the cal for the function above:
Code:
NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();
Console.WriteLine("Anzahl an Netzwerkkarten : {0}", nics.Length);
foreach (NetworkInterface adapter in nics)
{
if (adapter.NetworkInterfaceType.ToString() == "Ethernet")
{
if (adapter.Name.IndexOf("Drahtlose") < 0)
{
IPInterfaceProperties properties = adapter.GetIPProperties();
netzwerk.setIP("192.xxx.x.xx", "255.255.255.0", adapter.Name);
}
}
}
Many thanks for your help and best regards.