japogs007ph
New member
- Joined
- Oct 5, 2007
- Messages
- 2
- Programming Experience
- Beginner
Can someone help me how to get the ip addresses in a network?
Private Function GetIPAddresses() As ArrayList
Dim Addresses As ArrayList = New ArrayList() ' Temp array to fill
'Get my address
String MyAdd = Dns.GetHostByName(Dns.GetHostName()).AddressList(0).ToString()
' Used to start the command
Dim psi As New System.Diagnostics.ProcessStartInfo()
'prompt
psi.FileName = "C:\WINDOWS\system32\cmd.exe" ' Path for the cmd prompt
psi.Arguments = "/c net view > boo.txt" ' Arguments for the command prompt
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
Application.DoEvents()
' So the computer doesn't bog down
Dim Run As System.Diagnostics.Process.Start(psi)
Dim sr As System.IO.StreamReader = Nothing
Dim run As Boolean = False ' used to tell if dos is done yet
While run = False
Application.DoEvents()
Try
System.Threading.Thread.Sleep(1000)
sr = New System.IO.StreamReader(Application.StartupPath
+ "\\boo.txt") ' Try to open the file
' If dos is not done yet, this will throw an error
run = True ' If I get here, dos is done and we can use the file
Catch
run = False ' Dos is not done yet
End Try
End While
' Cycle through the file until the info starts
While sr.ReadLine().StartsWith("--") <> True
Application.DoEvents()
End While
Dim str As String = "" ' filled with each line of the file
Dim comp() As String = New String(32) ' temp array of computer names
Dim i As Integer = 0 ' indexer for comp[]
' cycle until we reach, "The command completed successfully."
While str.StartsWith("The") <> True
Application.DoEvents()
str = sr.ReadLine() ' Read the next line
' Split the line after the first space
comp(i) = str.Split(Char.Parse(" "))(0)
' Normally, in the file it lists it like this
' \\MyComputer My Computer's Description
' Take off the slashes, "\\MyComputer" to "MyComputer"
comp(i) = comp(i).Substring(2, comp(i).Length - 2)
If comp(i) = "e" Then
Application.DoEvents()
comp(i) = Nothing
End If
i += 1
End While
sr.Close()
sr = Nothing ' free memory
Dim s As String
For Each s In comp
If Not s Is Nothing Then
If s.ToUpper() <> Dns.GetHostName().ToUpper() Then
Addresses.Add(Dns.GetHostByName(s).AddressList(0).ToString())
' ... add the address to the array
End If
End If
Next
Return Addresses ' return the array
End Function