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.
Last edited by BioPhysEngr; March 6th, 2012 at 01:08 AM.
Reason: added code and /code tags
Please surround code segments with [code] and [/code] tags to preserve formatting and make it easier to read. I've fixed your current post for now.
Best Regards,
BioPhysEngr http://blog.biophysengr.net
--
All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.
@uwillfeelsorry1:
It would be also ok having a possibility to change all Ethernet-Adapters, except the wireless.
With this function I can identify the network-adapters with the needed information to know if itīs the right network adapter. But I have trouble to merge this codes together.
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)
{ netzwerk.setIP("192.168.1.11", "255.255.255.0", adapter.Name); }
}
}
Bookmarks