Solved

Get the MAC Address of the physical network adapter

Posted on 2008-06-12
6
853 Views
Last Modified: 2013-11-26
Dear all,
I'm looking for a way to find in VB NET the MAC Address of the network card installed on the pc, I mean not VPN, not any other kind of virtual network adapter, just the MAC of the card inside the pc, even if it's disabled.
Please see the function I wrote below, now I just avoid the NetworkInterfaceType=Tunnel, but how can I be sure to find the physical card? NetworkInterface type has so many values....
http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterfacetype.aspx
Public Function GetMacAddress() As String
        Dim theNetworkInterfaces() As System.Net.NetworkInformation.NetworkInterface =   System.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()
 
        Dim currentInterface As System.Net.NetworkInformation.NetworkInterface
 
        For Each currentInterface In theNetworkInterfaces
            If currentInterface.NetworkInterfaceType <>   Net.NetworkInformation.NetworkInterfaceType.Tunnel Then
                GetMacAddress = currentInterface.GetPhysicalAddress().ToString()
                Exit For
            End If
        Next
 
    End Function

Open in new window

0
Comment
Question by:aaresearch
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 

Author Comment

by:aaresearch
ID: 21767460
Well, the question is not so easy!
0
 
LVL 12

Expert Comment

by:renjurdevan
ID: 21767645
using System.Management;
 

string MacAddress = "";
                        try
                        {
                                ManagementClass myManagementClass = new
ManagementClass("Win32_NetworkAdapterConfiguration");
                                ManagementObjectCollection moc = myManagementClass.GetInstances();


                                foreach(ManagementObject mo in moc)
                                {
                                        MacAddress = mo["MacAddress"].ToString();
                                        break;
                                }
}
0
 
LVL 12

Expert Comment

by:renjurdevan
ID: 21767665
Imports System.Management

Dim MacAddress  As string

Dim myManagementClass  As New ManagementClass("Win32_NetworkAdapterConfiguration")

 Dim moc As ManagementObjectCollection = myManagementClass.GetInstances()

For Each ManagementObject mo in moc
MacAddress = mo("MacAddress").ToString()
Next

Console.WriteLine (MacAddress )

Make sure that System.Mangement dll included in your application

0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

Author Comment

by:aaresearch
ID: 21768272
Dear renjurdean,
I think you did not catch my question.

I have already found the solution to get ALL the MAC of all the network adapter (see the first code I posted), I just want to get the one of the card installed on the pc, only that one.
How can I be sure?

Thanks
0
 
LVL 12

Expert Comment

by:renjurdevan
ID: 21768315
moc as ManagementObjectCollection (Collection) so you should enumerate it!
foreach(ManagementObject mo in moc)
                                {
                                        MacAddress = mo["MacAddress"].ToString();
                                       break;
                               }


Check myManagementClass' s methods
0
 

Accepted Solution

by:
aaresearch earned 0 total points
ID: 21780074
I rather prefer checking the
currentInterface.NetworkInterfaceType
in the function I wrote, thank you

The exactly what I was trying to explain, that the propoerty had got lots of values, and I'm not sure about their meaning.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question