Hello
I've got three network adapter on my machine, and I'm looking to use WMI to
set the IP address of each apapter but I need some help. My network
adapters are Intel (R) Pro/100 VE, PRO/1000 CT, PRO/1000 MT and I want to
set the first to an IP of 192.168.100.100, the second NIC to
192.168.100.200, and the third to 192.168.100.300. The following code runs
without errors, but my IP addresses never change. Any idea's ?
Thanks
public void CheckAndSetIp()
{
ManagementClass MC = new
ManagementClass("Win32_NetworkAdapterConfiguration ");
ManagementObjectCollection MOC = MC.GetInstances();
foreach (ManagementObject obMO in MOC)
{
try
{
Console.WriteLine("In Loop");
ManagementBaseObject objNewIP = null;
ManagementBaseObject objSetIP = null;
// if true TCP/IP is enabled on this NIC
if (!(bool)obMO["ipEnabled"])
continue;
string Desc = (string)(obMO["Description"]);
if (Desc.Substring(0, 19) == "Intel(R) PRO/100 VE")
{
Console.WriteLine(Desc.Substring(0, 19)); //this
lines prints out
objNewIP =
obMO.GetMethodParameters("EnableStatic");
objNewIP["IPAddress"] = new string[] {
"192.168.100.100" };
objNewIP["SubnetMask"] = new string[] {
"255.255.255.0" };
objSetIP = obMO.InvokeMethod("EnableStatic",
objNewIP, null);
Console.WriteLine("IP Set ? "); //this lines
prints out
}
if (Desc.Substring(0, 20) == "Intel(R) PRO/1000 MT")
{
Console.WriteLine(Desc.Substring(0, 20)); //this
lines prints out
objNewIP =
obMO.GetMethodParameters("EnableStatic");
objNewIP["IPAddress"] = new string[] {
"192.168.100.200" };
objNewIP["SubnetMask"] = new string[] {
"255.255.255.0" };
objSetIP = obMO.InvokeMethod("EnableStatic",
objNewIP, null);
Console.WriteLine("IP Set ? "); //this lines
prints out
}
//if (Desc.Substring(0, 20) == "Intel(R) PRO/1000 CT")
//{
// Console.WriteLine(Desc.Substring(0, 20));
// objNewIP =
obMO.GetMethodParameters("EnableStatic");
// objNewIP["IPAddress"] = new string[] {
"192.168.100.300"};
// objNewIP["SubnetMask"] = new string[] {
"255.255.255.0" };
// objSetIP = obMO.InvokeMethod("EnableStatic",
objNewIP, null);
//}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
}
Console.WriteLine("End for each loop");
} //end CheckAndSetIp