troubles Setting IP address for each NIC

B

Billy Bob

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
 
D

David Browne

Billy Bob said:
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 ?

Yes. Visual Studio will create a wrapper type for the management class,
making this a whole lot easier.

With your project open in Visual Studio, open the "Server Explorer", Expand
Servers/[Your Computer Name]/Management Classes, right-click on "Network
Adapter Settings" and choose "Generate Managed Class". This will create a
type called ROOT.CIMV2.Win32_NetworkAdapterConfiguration in your project.
Then just use that type like this:

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Collections;
using System.Diagnostics;
using csTestLib.ROOT.CIMV2;

namespace Test
{
public class Program
{
public static void Main(string[] args)
{
try
{
foreach (NetworkAdapterConfiguration a in
NetworkAdapterConfiguration.GetInstances())
{
Console.WriteLine("In Loop - " + a.Description);
if (!a.IPEnabled)
continue;

if (a.Description == "Microsoft Loopback Adapter")
{
a.EnableStatic(new string[] { "10.39.0.10" },
new string[] { "255.255.0.0" });
}
}

}
catch (Exception ex)
{
Console.WriteLine(ex);
}
Console.WriteLine("Hit any key to exit");
Console.ReadKey();
}
}


}

David
 
J

Jonathan Woods

Hi Billy,

Have you ever use IP Helper API? I guess Codeproject has got samples.
Please have a look over there.
 
K

KWienhold

Just thought I'd point this out, though it has nothing to do with your
code:
The IP-Adress "192.168.100.300" is not technically possible (since 300
won't fit in a byte).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top