473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

troubles Setting IP address for each NIC

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
3 6020


"Billy Bob" <wh*@microsoft.comwrote in message
news:eQ**************@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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Damien | last post by:
Hi to all, I'm currently re-designing our intranet : nice and lean CSS2, cleaned-up PHP 4.3.7, better-normalized MySQL ;o). So I've started using the $_SESSION variable instead of register_globals...
3
by: Mr. B | last post by:
GRRRR... I've run across a situation in which I have NO solution. Hopefully there is one. VB.net. It's rather simple. I've a ComboBox that get's populated via a Database. And I pre-select...
9
by: Kelly Vernon | last post by:
I have a standard ASP page that appends to an xml page. Currently if there is more than one person attempting to append to the same XML file at a time. One user will have the ability to append,...
5
by: dixie | last post by:
I want to be able to set the font size and font type for text in a text box on a report using VBA. I wan't to be able to control it from a setting in a table. The problem is that I don't know the...
8
by: google_groups3 | last post by:
Below is a snippet of code I am using. I want to set the IP, subnet and default gateway of my local computer via code. However, when I do it, I get a "not found" (error number 5) error on the...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
2
by: Daniele C. | last post by:
I will report a snippet from http://php.net/setlocale I spent about 2 seconds before thinking: can it really be? I mean, is there no way to set locale info per thread? And after a brief web...
0
by: sarahnetworking | last post by:
Hello, I have developed a client server application using C. i have a list of usernames and a list of passwords in a text file. i need to be able to do 2 loops so i can send the usernames and...
1
by: sidathkp | last post by:
Here is a sample of the xml file that I have: <provider> <cinames> <cinema> <id>1111</id> <name>ABC cinema</name> <address>123 street</address>...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.