Click to See Complete Forum and Search --> : help


blazerguns
September 16th, 2006, 12:43 PM
Hi all,

I have used WMI method to set ipaddress in my system. But the problem is that if i have multiple iface cards this program is setting all the nic to same ip. I dont know how to stop this from happening. I have my code below. Please help to identify single iface to set that all.

public void setIP(string IPAddress, string SubnetMask, string Gateway)
{
ManagementClass objMC = new ManagementClass("Win32_NetworkAdapterConfiguration");
ManagementObjectCollection objMOC = objMC.GetInstances();

//objMC.GetInstances(
foreach (ManagementObject objMO in objMOC)
{
if (!(bool)objMO["IPEnabled"])
continue;

try
{
ManagementBaseObject objNewIP = null;
ManagementBaseObject objSetIP = null;
ManagementBaseObject objNewGate = null;
objNewIP = objMO.GetMethodParameters("EnableStatic");
objNewGate = objMO.GetMethodParameters("SetGateways");

objNewGate["DefaultIPGateway"] = new string[] { Gateway };
objNewGate["GatewayCostMetric"] = new int[] { 1 };
objNewIP["IPAddress"] = new string[] { IPAddress };
objNewIP["SubnetMask"] = new string[] { SubnetMask };
objSetIP = objMO.InvokeMethod("EnableStatic", objNewIP, null);
objSetIP = objMO.InvokeMethod("SetGateways", objNewGate, null);

//Console.WriteLine("Updated IPAddress, SubnetMask and Default Gateway!");
}
catch (Exception ex)
{
//Console.WriteLine("Unable to Set IP : " + ex.Message);
/*Error check*/

}

}

Its very urgent please help.

Varun