Richárd Kiss
Richárd Kiss

Reputation: 11

VB net forearch into one text

I made an app in vb.net which show the interface's mac address.

Dim computerProperties As IPGlobalProperties = IPGlobalProperties.GetIPGlobalProperties()
Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
If nics Is Nothing OrElse nics.Length < 1 Then
    Console.WriteLine("  No network interfaces found.")
    Exit Sub
End If
For Each adapter As NetworkInterface In nics
    If adapter.GetPhysicalAddress.ToString.Length > 1 Then
        If adapter.GetPhysicalAddress.ToString.Contains("000") Then
        Else
            MsgBox(adapter.GetPhysicalAddress.ToString())
        End If
    End If
Next

I want to put the mac addresses into one string like this "00ffg344f2-33f5h6g3-...." How can I make this?

Upvotes: 0

Views: 63

Answers (2)

Youssef13
Youssef13

Reputation: 4954

Dim L As New List(Of String)
Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
If nics Is Nothing OrElse nics.Length < 1 Then
    Console.WriteLine("  No network interfaces found.")
    Exit Sub
End If

For i = 0 To nics.Count - 1
    If nics(i).GetPhysicalAddress.ToString.Length > 1 Then If Not nics(i).GetPhysicalAddress.ToString.Contains("000") Then L.Add(nics(i).GetPhysicalAddress.ToString())
Next
MsgBox(String.Join("-", L.ToArray()))

Try to use the previous code , I think that's what exactly you want. Another shorter solution:

Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
If nics Is Nothing OrElse nics.Length < 1 Then
    Console.WriteLine("  No network interfaces found.")
    Exit Sub
End If

Dim OutputArr() As String = nics.Cast(Of NetworkInterface).Select(Function(x) x.GetPhysicalAddress.ToString()).Where(Function(x) x.Length > 1).ToArray()
MsgBox(String.Join("-", OutputArr))

Upvotes: 0

Dai
Dai

Reputation: 155225

Dim sb As New StringBuilder()

For Each adapter As NetworkInterface In nics

    If adapter.GetPhysicalAddress.ToString.Length > 1 Then

        If Not adapter.GetPhysicalAddress.ToString.Contains("000") Then

            sb.Append( adapter.GetPhysicalAddress.ToString() )
            sb.Append( vbCrLf )
        End If
    End If
Next

MsgBox( sb.ToString() )

Upvotes: 1

Related Questions