VB.net Reliable way to always return the same code for the MACAddress

Hi

The following function always returned my machine's MAC addresss but suddenly returns a blank string. Why might the cause be?

    Friend Function GetMACAddress() As String

        Dim mc As ManagementClass = New ManagementClass("Win32_NetworkAdapterConfiguration")
        Dim moc As ManagementObjectCollection = mc.GetInstances()
        Dim MACAddress As String = String.Empty
        For Each mo As ManagementObject In moc

            If (MACAddress.Equals(String.Empty)) Then
                If CBool(mo("IPEnabled")) Then MACAddress = mo("MacAddress").ToString()

                mo.Dispose()
            End If
            MACAddress = MACAddress.Replace(":", String.Empty)

        Next
        Return MACAddress
    End Function

Open in new window


The function below seems to work better but it said that that  only refers to the first network adapter
    Function getMacAddress()
        Dim nics() As NetworkInterface = _
              NetworkInterface.GetAllNetworkInterfaces
        Return nics(0).GetPhysicalAddress.ToString
    End Function

Open in new window



What is a reliable way to always return the same code for the MACAddress
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
CodeCruiserConnect With a Mentor Commented:
One option then is to get MAC addresses of all the physical NICs in user's computer and then if any of the MACs matches with the one that you have in your database then its a known user.
0
 
CodeCruiserCommented:
Problem with your first function is that it loops through the network adaptors and returns you MAC of the last adaptor in the loop. Problem with second function is that it always returns MAC of the first adaptor. What is your requirement? Always return the same MAC? Or always return some MAC?
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi
It is to always return the same MAC
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
CodeCruiserCommented:
I don't know the context of your program but may be you run this function to get a valid MAC once and then store the MAC somewhere instead of calling this function every time.
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Hi
I am using the MAC address as a way to identify a valid user so it is stored in my cloud SQL
database. Each time the user connects my code validates that the MAC address is the same. So I am just looking for a way to return the same MAC address
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
thanks very much
0
 
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.