|
-
Aug 15th, 2011, 01:45 PM
#1
Thread Starter
Junior Member
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
-
Aug 15th, 2011, 01:52 PM
#2
Re: Output of Array
vb Code:
Imports System.Net Imports System.Net.NetworkInformation Public Class Form1 Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim gateway_addresses() As IPAddress = EnumGateWays() Dim str As String = "" For Each addr As IPAddress In gateway_addresses str &= addr.ToString & Environment.NewLine Next MsgBox(str) End Sub Private Function EnumGateWays() As IPAddress() Dim ret As New List(Of IPAddress) Dim adapters() As NetworkInterface = NetworkInterface.GetAllNetworkInterfaces Dim adapterinfo As IPInterfaceProperties = Nothing Dim gateways As GatewayIPAddressInformationCollection = Nothing For Each adapter As NetworkInterface In adapters adapterinfo = adapter.GetIPProperties gateways = adapterinfo.GatewayAddresses ret.AddRange((From g As GatewayIPAddressInformation In gateways Select g.Address).ToArray()) Next Return ret.ToArray End Function End Class
-
Aug 15th, 2011, 02:06 PM
#3
Thread Starter
Junior Member
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
-
Aug 15th, 2011, 02:20 PM
#4
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
 
-
Aug 15th, 2011, 02:35 PM
#5
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|