?
Solved

Get the MAC Address of the physical network adapter

Posted on 2008-06-12
6
Medium Priority
?
860 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
Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

 

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 …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

718 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