0

Visual Basic - I need to work with a List of things - an array, something. But my brain is Practical and the examples of ArrayList, List(of things), Dictionaries, etc. all are just creating the list, not working with it. So, problem is I need to Run IPConfig /all and pull that into the program, then go through it and make something (array? list of things?) that will let me go back and reference items in the list... Nic1.Name = Eth0 ; Nic1.MediaState = Disconnected ; Nic1.Description = Intel Adapter ; Nic1.PhysicalAddress = 00.3f.dc.3f.qt ... etc... Nic2, Nic3, etc.

Then work with it as ... Select Nic where nic.name like "ethernet" - put nic.dhcpenabled value in listbox1, etc. (yes, I do more powershell than visual basic these days)

I've got a Class set up:

Public Class Nics
    Public Property Name As String
    Public Property MediaState As String
    Public Property Description As String
    Public Property PhysicalAddress As String
    Public Property DHCPEnabled As String
    Public Property IPv4 As String
    Public Property DefaultGateway As String
    Public Property DNSServer1 As String
    Public Property NetBiosoverTcpIP As String
End Class

and am pulling in the results of the ipconfig /all to an array of lines

Dim cOutput As String
        Using oStreamReader As System.IO.StreamReader = cProcess.StandardOutput
            cOutput = oStreamReader.ReadToEnd()
        End Using
        Dim aIpConfig() As String = cOutput.Split(Environment.NewLine)

and go into a For Each to parse the lines...

Dim mNics As New List(Of Nics) ' ???
   For Each vLine In aIpConfig
            ' MsgBox(vLine)
            ' Build out an array with each NIC and it's info... 
            If vLine Like "Host Name*" Then
                Dim anHN() As String = vLine.Split(":")
                Dim nHostName As String = anHN(1)
                NICListInfo.Items.Add("Host Name: " & nHostName)
            End If
            If vLine Like "DNS Suffix*" Then
                Dim anSuffixes() As String = vLine.Split(":")
                NICListInfo.Items.Add("DNS Suffixes: " & anSuffixes(1))
            End If

            If (Right(vLine, 2)) Like ("*:") Then
                ' THis means it is a NIC!
                ' MsgBox(vLine)
                Dim anNic() As String = vLine.Split(":")
                mNics.Add() '????
            End If
            If vLine Like "Media State*" Then
                Dim anNic() As String = vLine.Split(":")
                mNics(cNics, aNicInfo) = "Media State: " & anNic(1)
                ' and do WHAT now?

It is for a GUI where it will go into two listboxes - one with a list of Nic Names, the user clicks on the nic and listbox2 shows the relevant info for that Nic Name.

Clemens
  • 123,504
  • 12
  • 155
  • 268
Graywalker
  • 25
  • 2
  • Getting a little closer with : If (Right(vLine, 2)) Like ("*:") Then ' THis means it is a NIC! ' MsgBox(vLine) Dim anNic() As String = vLine.Split(":") mNics.Add(New Nics()) mNics(mNicCount).Name = anNic(0) MsgBox(mNics(mNicCount).Name) mNicCount += 1 End If – Graywalker Mar 18 '23 at 21:25
  • I think you can try read this information without text parsing. Does this answer your question? [Parsing windows 'ipconfig /all' output](https://stackoverflow.com/questions/15184495/parsing-windows-ipconfig-all-output) – Lukasz Szczygielek Mar 19 '23 at 18:07

1 Answers1

0

So, I worked it out, but still open to suggestions on how to make it quicker and cleaner.
Taking the Count of the List(of things) to set the properties and can do a For Each to go through them for a match of the .name property. Doing a MsgBox to prove it works, but can populate the listboxes with it. Its a bit clunky, but it works.

         If (Right(vLine, 2)) Like ("*:") Then
                ' THis means it is a NIC!
                ' MsgBox(vLine)
                Dim anNic() As String = vLine.Split(":")
                mNics.Add(New Nics())
                mNicCount = (mNics.Count) - 1
                mNics(mNicCount).Name = anNic(0)
                'MsgBox(mNics(mNicCount).Name)
            End If


            If vLine Like "Media State*" Then
                Dim anNic() As String = vLine.Split(":")
                mNics(mNicCount).MediaState = "Media State: " & anNic(1)
            End If
.... blah blah
       Next     

      For Each mnic In mNics
            MsgBox(mnic.Name & " - " & mnic.Description & " - " & mnic.IPv4 & " - " & mnic.NetBiosoverTcpIP)
        Next

You can see that I do Message Boxes for troubleshooting, to make sure what I am expecting is coming through, and I just comment them out and leave them there for future troubleshooting. :D

Graywalker
  • 25
  • 2