A urgent help

  • Thread starter Thread starter blazerguns
  • Start date Start date
B

blazerguns

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
 
Hello blazerguns,

You need to know the name of you network adapter and then use

SELECT * FROM Win32_NetworkAdapterConfiguration where Description = "your
network adapter name"



b> Hi all,
b>
b> I have used WMI method to set ipaddress in my system. But
b> the problem is that if i have multiple iface cards this program is
b> setting all the nic to same ip. I dont know how to stop this from
b> happening. I have my code below. Please help to identify single iface
b> to set that all.
b>
b> public void setIP(string IPAddress, string SubnetMask, string
b> Gateway)
b> {
b> ManagementClass objMC = new
b> ManagementClass("Win32_NetworkAdapterConfiguration");
b> ManagementObjectCollection objMOC = objMC.GetInstances();
b> //objMC.GetInstances(
b> foreach (ManagementObject objMO in objMOC)
b> {
b> if (!(bool)objMO["IPEnabled"])
b> continue;
b> try
b> {
b> ManagementBaseObject objNewIP = null;
b> ManagementBaseObject objSetIP = null;
b> ManagementBaseObject objNewGate = null;
b> objNewIP =
b> objMO.GetMethodParameters("EnableStatic");
b> objNewGate =
b> objMO.GetMethodParameters("SetGateways");
b> objNewGate["DefaultIPGateway"] = new string[] {
b> Gateway };
b> objNewGate["GatewayCostMetric"] = new int[] { 1
b> };
b> objNewIP["IPAddress"] = new string[] { IPAddress
b> };
b> objNewIP["SubnetMask"] = new string[] {
b> SubnetMask
b> };
b> objSetIP = objMO.InvokeMethod("EnableStatic",
b> objNewIP, null);
b> objSetIP = objMO.InvokeMethod("SetGateways",
b> objNewGate, null);
b> //Console.WriteLine("Updated IPAddress,
b> SubnetMask
b> and Default Gateway!");
b> }
b> catch (Exception ex)
b> {
b> //Console.WriteLine("Unable to Set IP : " +
b> ex.Message);
b> /*Error check*/
b> }
b>
b> }
b>
b> Its very urgent please help.
b>
b> Varun
b>
---
WBR,
Michael Nemtsev :: blog: http://spaces.live.com/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsch
 
You need to use a select query like:

select * from Win32_NetworkAdapterConfiguration where Description='adapter
description string' AND IPEnabled=true

this will return only a single adapter (provided he's IP enabled), so, there
is no need to perform this..

| if (!(bool)objMO["IPEnabled"])
| continue;
|

Willy.

| 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
|
 
Back
Top