Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2006

Tip: Looking for answers? Try searching our database.

Enumerating MAC addresses

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bob Altman - 21 Dec 2005 02:23 GMT
Hi all,

I'm looking for a way to enumerate the MAC addresses of all of "live" NICs
on my computer.  I can enumerate all of the "live" IP addresses like this:

   Dns.GetHostByName(Dns.GetHostName).AddressList

but I don't know how to get the associated MAC addresses.

Here's the problem I'm actually trying to solve:  When my app runs, I want
it to always talk to the same network wire, even if the host IP address on
that wire changes.  My solution is to enumerate the network connections
(using code like that shown above) when the app starts.  If there are more
than one connection then I would prompt the user for the connection to use,
and I would remember the MAC address so that I don't need to bother the user
the next the app is run (even if DHCP gives the computer a new IP address).

TIA!

 - Bob
Remy - 21 Dec 2005 04:35 GMT
I think that should solve it:
http://www.codeproject.com/csharp/Host_Info_within_Network.asp

(from the page)
The physical MAC address is collected using the DLL "iphlpapi.dll" with
the API SendARP().

For each System, we use the Hostname to retrieve the MAC address.

//    [DllImport("iphlpapi.dll", ExactSpelling=true)]
//    public static extern int SendARP( int DestIP, int SrcIP,
//         [Out] byte[] pMacAddr, ref int PhyAddrLen );

Cheers

Remy Blaettler
<a href="http://www.collaboral.com">www.collaboral.com</A>
Bob Altman - 21 Dec 2005 04:51 GMT
Thanks Remy!  While waiting for a reply to my posting, I dug through the
docs and put together this solution to my problem using WMI:

Imports System.Management
Private Sub Test()
 ' Ask WMI to fetch the network adapter info
 ' for enabled adapters
 Dim WMISearcher As New ManagementObjectSearcher( _
   "Select MACAddress, IPAddress " & _
   "from Win32_NetworkAdapterConfiguration " & _
   "where IPEnabled=TRUE")

 For Each mo As ManagementObject In WMISearcher.Get()
   Dim ipAddrs() As String = DirectCast(mo("IPAddress"), String())
   Dim macAddr As String = CStr(mo("MACAddress"))

   ' Each adapter has one MAC address and an array of IP addresses
   For Each ipAddr As String In ipAddrs
     Debug.WriteLine(macAddr & " - " & ipAddr)
   Next
 Next
End Sub
John Vorchak eMVP - 12 Mar 2006 18:20 GMT
> Thanks Remy!  While waiting for a reply to my posting, I dug through the
> docs and put together this solution to my problem using WMI:

Instead of using WMI to do this, you can perform this through the Net and
NetworkInformation class.  This is a portion of a piece of code that I'm
currently working on that I just happen to need managed call to the MAC (I
ripped this out of my code and tossed it into a console app so it's not
exactly neat...):

//*****Start C# Snippet
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Net.NetworkInformation;

namespace getMac
{
   class Program
   {
       static void Main(string[] args)
       {
           
           int adaptNum = 0;
           NetworkInterface[] adapters =
NetworkInterface.GetAllNetworkInterfaces();
           foreach (NetworkInterface adapter in adapters)
           {
               IPInterfaceProperties adapterProperties =
adapter.GetIPProperties();
               UnicastIPAddressInformationCollection uniCast =
adapterProperties.UnicastAddresses;

               foreach (IPAddressInformation uni in uniCast)
               {
                   //increment adapt number
                   adaptNum++;
                   //get mac
                   System.Net.NetworkInformation.PhysicalAddress mac =
adapter.GetPhysicalAddress();
                   Console.WriteLine("Adapter #{0} -------- {1}",
adaptNum.ToString(), mac.ToString());
               }
           }
       }
   }
}
//*****End C# Snippet

One word of caution, this will bring back everything, including loopback
adapter, so make sure you build handling in there for empty MACs.

John Vorchak eMVP
Peter Franks - 22 Dec 2005 16:08 GMT
> Hi all,
>
> I'm looking for a way to enumerate the MAC addresses of all of "live" NICs
> on my computer.  I can enumerate all of the "live" IP addresses like this:

static string[] MacAddresses
{
  get
  {
    ArrayList macAddresses = new ArrayList();

    ManagementObjectSearcher query = new
ManagementObjectSearcher("SELECT * FROM
Win32_NetworkAdapterConfiguration");
    foreach (ManagementObject each in query.Get())
    {
      string macAddress = each["MACAddress"] != null ?
each["MACAddress"].ToString() : null;
      bool ipEnabled = (bool)each["IPEnabled"];
      if (macAddress != null && ipEnabled)
      {
        if (!macAddresses.Contains(macAddress))
        {
          macAddresses.Add(macAddress);
        }
      }
    }
    return macAddresses.Count > 0 ?
(string[])macAddresses.ToArray(typeof(string)) : null;
  }
}

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.