Murray Brown
asked on
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?
The function below seems to work better but it said that that only refers to the first network adapter
What is a reliable way to always return the same code for the MACAddress
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
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
What is a reliable way to always return the same code for the MACAddress
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?
ASKER
Hi
It is to always return the same MAC
It is to always return the same MAC
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.
ASKER
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
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks very much
ASKER
HI
I posed a further related question on this:
https://www.experts-exchange.com/questions/28376847/VB-net-Get-all-MAC-addresses-on-a-computer.html
I posed a further related question on this:
https://www.experts-exchange.com/questions/28376847/VB-net-Get-all-MAC-addresses-on-a-computer.html