PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Output of Array-VBForums
Results 1 to 5 of 5

Thread: Output of Array

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Posts
    18

    Output of Array

    To whom it may concern,

    In my application, the user clicks a single button and the program displays each network adapter description into a message box and then gets all the ip info for the workstation and places it into a text file and opens that text for reading. What I would really like for it to do would be to display all the network adapters into a single messagebox instead of messagebox for every adapter like it is currently doing. Here is the code I have on that button. Let me know if anyone needs more info or has any questions:

    Dim nics As NetworkInterface() = NetworkInterface.GetAllNetworkInterfaces()
    If nics Is Nothing OrElse nics.Length < 1 Then
    MessageBox.Show("No network interfaces found.")
    Else : Call Shell("cmd /c ipconfig /all >c:\ipconfig.txt")
    For Each adapter As NetworkInterface In nics
    MessageBox.Show(adapter.Description)
    Next
    System.Threading.Thread.Sleep(500)
    Process.Start("c:\ipconfig.txt")
    Exit Sub
    End If


    Thanks in advance,
    Scott

  2. #2
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Output of Array

    vb Code:
    1. Imports System.Net
    2. Imports System.Net.NetworkInformation
    3.  
    4. Public Class Form1
    5.  
    6.  
    7.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    8.         Dim gateway_addresses() As IPAddress = EnumGateWays()
    9.  
    10.         Dim str As String = ""
    11.  
    12.         For Each addr As IPAddress In gateway_addresses
    13.             str &= addr.ToString & Environment.NewLine
    14.         Next
    15.  
    16.         MsgBox(str)
    17.     End Sub
    18.  
    19.     Private Function EnumGateWays() As IPAddress()
    20.         Dim ret As New List(Of IPAddress)
    21.         Dim adapters() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces
    22.         Dim adapterinfo As IPInterfaceProperties = Nothing
    23.         Dim gateways As GatewayIPAddressInformationCollection = Nothing
    24.  
    25.         For Each adapter As NetworkInterface In adapters
    26.             adapterinfo = adapter.GetIPProperties
    27.             gateways = adapterinfo.GatewayAddresses
    28.  
    29.             ret.AddRange((From g As GatewayIPAddressInformation In gateways Select g.Address).ToArray())
    30.         Next
    31.         Return ret.ToArray
    32.     End Function
    33. End Class

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Posts
    18

    Re: Output of Array

    Cicatrix,

    Your code works, but it only displays the ip address of one of the nics. What I am wanting idealy is to display the name of all the network adapters into a message box. The ip thing I have worked out and can pull that without issue, but thank you for trying to help me nonetheless.

    Thanks again,
    Scott

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    34,565

    Re: Output of Array

    Still, what he showed should be enough if the code you showed was working. The key is that you want to append all your strings together in the loop to create one large string, and show that. Concatenating in the NewLine character will make it look a bit nicer, too. Take this part of his code:
    Code:
    For Each addr As IPAddress In gateway_addresses            
     str &= addr.ToString & Environment.NewLine        
    Next
    And merge the concept of that into the loop you already have. He is building a string to display, and so should you.

    By the way, this will be ok for a certain number of nics, but once you get beyond some size, the messagebox will be too ugly to show. You might consider using a form with a listbox on it to display this information rather than a messagebox.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2011
    Posts
    18

    Re: Output of Array

    My apologies to you, Cicatrix. You were on point. I guess I just needed it explained a little more. However, once I incorporated your code with what I had it worked like a charm.

    Also, thank you to you as well, Shaggy Hiker.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width