Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Get the MAC Address of the physical network adapter

Posted on 2008-06-12
6
Medium Priority
?
868 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

916 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