WMI in my c# app to get mac & IP address

  • Thread starter Thread starter Jason
  • Start date Start date
J

Jason

Hello

I'm writting a c# app, and I've got a computer that has two built in nic's.
One is a Intel Pro/100 and the other is a Intel Pro/1000.

I want to get the mac and ip address assigned to the Intel Pro 100.

Anyone have idea, examples of how I can get that information to display to a
user?
 
Jason said:
Hello

I'm writting a c# app, and I've got a computer that has two built in
nic's. One is a Intel Pro/100 and the other is a Intel Pro/1000.

I want to get the mac and ip address assigned to the Intel Pro 100.

Anyone have idea, examples of how I can get that information to display to
a user?

Open Server Explorer, expand your computer, expand Management Classes,
right-click on the desired WMI class and generate a managed wrapper for it.

David
 
Ok, I got that much, but it looks like I'm going to need a bit more help.
Can I use that as a class to get the ionfo I need for my particular network
adapter?
 
Jason said:
Ok, I got that much, but it looks like I'm going to need a bit more help.
Can I use that as a class to get the ionfo I need for my particular
network adapter?

Then do a seach on "wmi ip mac" and discover that that information is under
the Win32_NetworkAdapter class. And find the following snippet of code
 
David Browne said:
Then do a seach on "wmi ip mac" and discover that that information is
under the Win32_NetworkAdapter class. And find the following snippet of
code

oops

strComputer = "."
Set objWMIService = GetObject(_
"winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapter " _
& "Where NetConnectionID = " & _
"'Local Area Connection 2'")

For Each objItem in colItems
strMACAddress = objItem.MACAddress
Next

Set colItems = objWMIService.ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration")

For Each objItem in colItems
If objItem.MACAddress = strMACAddress Then
For Each strIPAddress in objItem.IPAddress
Wscript.Echo "IP Address: " & strIPAddress
Next
End If
Next


Which tells you the classes you will need to use. Aparently you need to use
Win32_NetworkAdapter to enumerate the nic's and then match each nic with its
Win32_NetworkAdapterConfiguration by matching the MAC address. In the
server explorer you need so fumble around and add an additional class (right
click on Management Classes, add class). And find
Win32_NetworkAdapterConfiguration under root\CIMV2\Network Adapter Settings.
Add managed wrappers for both classes, and then write:



using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using csTest.ROOT.CIMV2;



namespace csTest
{

class Program
{


public static void Main(string[] args)
{
Dictionary<string,NetworkAdapterConfiguration> configs =
new Dictionary<string,NetworkAdapterConfiguration>();

foreach (NetworkAdapterConfiguration config in
NetworkAdapterConfiguration.GetInstances())
{
if (config.MACAddress != null)
{
configs.Add(config.MACAddress, config);
}
}

foreach (NetworkAdapter nic in NetworkAdapter.GetInstances())
{
Console.WriteLine(string.Format("NIC {0} MAC
{1}",nic.Name,nic.MACAddress));

if (nic.MACAddress != null)
{
NetworkAdapterConfiguration config = configs[nic.MACAddress];

string[] addresses = config.IPAddress;
if (addresses == null)
{
addresses = new string[0];
}
foreach (string ip in addresses)
{
Console.WriteLine(string.Format(" Address {0}",
config.IPAddress));
}
}

}
}
}

}


Piece of cake, huh :). But you can get to everything, I mean everything,
using WMI.

David
 

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

Back
Top