Solved

Get the MAC Address of the physical network adapter

Posted on 2008-06-12
6
855 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
Industry Leaders: 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!

 

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

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AJAX Create / Update Not working MVC 5 64
GET INFO ABOUT WHAT THE PRINTER IS DOING ? 11 76
VB Catch Statement in Event Viewer. 2 20
Subtract dates in vb.net 6 34
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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