VB.NET - getting mac add

Asked By SVK N on 29-Nov-12 09:44 AM
 Dim nic As Net.NetworkInformation.NetworkInterface
            Dim keyadd As String
            For Each nic In Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces
                keyadd = nic.GetPhysicalAddress.ToString


                If keyadd = "" Then
                    MsgBox("no add")
                Else
                    MsgBox(keyadd)


                End If
            Next





i need to get the mac add of the system . i have the above code
i do get the mac add but in the loop i get more than one mac add and some time blank also.


i need to get a single unique mac add which is unique to each system
Hemanth Kumar replied to SVK N on 03-Dec-12 04:15 AM
Hi SVk,
 Try like this
Dim theManagementScope as new ManagementScope("\\" & ComputerName & "\root\cimv2")
Dim theQueryString as new string = "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = 1"
  
Dim theObjectQuery as new ObjectQuery(theQueryString)
  
Dim theSearcher as new ManagementObjectSearcher(theManagementScope, theObjectQuery)
Dim theResultsCollection as ManagementObjectCollection = theSearcher.Get()
  
for each currentResult as ManagementObject in theResultsCollection
   MessageBox.Show(currentResult("MacAddress").ToString())
next