395,251 Members | 3,802 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 395,251 IT Pros & Developers. It's quick & easy.

troubles Setting IP address for each NIC

Billy Bob
P: n/a
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
Mar 3 '07 #1
Share this Question
Share on Google+
3 Replies


David Browne
P: n/a


"Billy Bob" <who@microsoft.comwrote in message
news:eQv$1CcXHHA.1296@TK2MSFTNGP02.phx.gbl...
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

Mar 3 '07 #2

Jonathan Woods
P: n/a
Hi Billy,

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

Mar 4 '07 #3

KWienhold
P: n/a
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).

Mar 5 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.