|
-
Feb 5th, 2013, 01:18 PM
#1
Thread Starter
New Member
VS2010 - IP Monitor Program Question
Hey guys. I'm trying to build a program that can tell me if Lan, Wan1, and Wan2 are up or down on each of our offices. The Office and IP information are coming from a text file and I'm using listview to display the office and IPs. The format of the text file is like this:
San Jose, 192.x.x.x, 66.x.x.x, 209.x.x.x
San Francisco, 192.x.x.x, 66.x.x.x, 209.x.x.x
Sacramento, 192.x.x.x, 66.x.x.x, 209.x.x.x
Now what I want is for the IP font color to be green if there is network connection and it turns red if there is no network connection. I was wondering if you guys can give me some pointers on how I can achive this. Thanks.
Code:
Dim Office As String
Dim Lan As String
Dim Wan1 As String
Dim Wan2 As String
Try
Dim sr As StreamReader
Dim fs As FileStream
fs = New FileStream(("C:\offices.txt"), FileMode.OpenOrCreate)
sr = New StreamReader(fs)
Dim itm As Object
itm = sr.ReadLine
While Not itm = Nothing
Dim split As String() = itm.split(New [Char]() {","})
Office = split(0)
Lan = split(1)
Wan1 = split(2)
Wan2 = split(3)
lvMain.Items.Add(Office)
lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Lan)
lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Wan1)
lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Wan2)
itm = sr.ReadLine
End While
sr.Close()
fs.Close()
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message, "Load Tool Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End Sub
-
Feb 5th, 2013, 01:21 PM
#2
Re: VS2010 - IP Monitor Program Question
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Feb 5th, 2013, 03:30 PM
#3
Thread Starter
New Member
Re: VS2010 - IP Monitor Program Question
Thanks dunfiddlin.
I don't know if I'm doing this right but I'm trying to get the green and red color on the Lan, for now but I get An unhandled exception of type 'System.Net.NetworkInformation.PingException' occurred in System.dll when starting the program.
Code:
Dim Office As String
Dim Lan As String
Dim Wan1 As String
Dim Wan2 As String
Try
Dim sr As StreamReader
Dim fs As FileStream
fs = New FileStream(("C:\users\albertc\desktop\offices.txt"), FileMode.OpenOrCreate)
sr = New StreamReader(fs)
Dim itm As Object
itm = sr.ReadLine
While Not itm = Nothing
Dim split As String() = itm.split(New [Char]() {","})
Office = split(0)
Lan = split(1)
Wan1 = split(2)
Wan2 = split(3)
lvMain.Items.Add(Office)
lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Lan)
lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Wan1)
lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Wan2)
itm = sr.ReadLine
End While
sr.Close()
fs.Close()
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message, "Load Tool Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
If My.Computer.Network.Ping(lvmain.Items(0).SubItems(1).Text) Then
lvmain.Items(0).SubItems(1).ForeColor = Color.Green
Else
lvmain.Items(0).SubItems(1).ForeColor = Color.Red
End If
End Sub
-
Feb 5th, 2013, 05:03 PM
#4
Re: VS2010 - IP Monitor Program Question
Looks like you're leaving a space in the address string in the split so effectively it's like ...
My.Computer.Network.Ping(" 192.168.0.1")
Try trimming the string before you use it.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Feb 7th, 2013, 01:24 PM
#5
Thread Starter
New Member
Re: VS2010 - IP Monitor Program Question
Thanks dundifflin. That helped. I removed the space and I added a new column for camera so it goes like this
Office,Lan,Wan1,Wan2,Camera
San Jose,192.x.x.x,65.x.x.x,66.x.x.x,192.x.x.x
So far, I have it working but it only works for row 1. I can make it work for row 2 but i have to repeat the same code as row one but just change the listview.items(0) to listview.items(1). The code can get really long if I'm having to do this for 22 offices. How do I simplify this? Any help would be greatly appreciated. Thanks.
Code:
Dim Office As String
Dim Lan As String
Dim Wan1 As String
Dim Wan2 As String
Dim Camera As String
Try
Dim sr As StreamReader
Dim fs As FileStream
fs = New FileStream(("C:\users\albertc\desktop\offices.txt"), FileMode.OpenOrCreate)
sr = New StreamReader(fs)
Dim itm As Object
itm = sr.ReadLine
While Not itm = Nothing
Dim split As String() = itm.split(New [Char]() {","})
Office = split(0)
Lan = split(1)
Wan1 = split(2)
Wan2 = split(3)
Camera = split(4)
lvMain.Items.Add(Office)
lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Lan)
lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Wan1)
lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Wan2)
lvmain.Items(lvmain.Items.Count - 1).SubItems.Add(Camera)
itm = sr.ReadLine
End While
sr.Close()
fs.Close()
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message, "Load Tool Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
'Row One
lvmain.Items(0).UseItemStyleForSubItems = False
'Lan
If My.Computer.Network.Ping(lvmain.Items(0).SubItems(1).Text) Then
lvmain.Items(0).SubItems(1).ForeColor = Color.Green
Else
lvmain.Items(0).SubItems(1).ForeColor = Color.Red
End If
'Wan1
If My.Computer.Network.Ping(lvmain.Items(0).SubItems(2).Text) Then
lvmain.Items(0).SubItems(2).ForeColor = Color.Green
Else
lvmain.Items(0).SubItems(2).ForeColor = Color.Red
'Wan2
End If
If My.Computer.Network.Ping(lvmain.Items(0).SubItems(3).Text) Then
lvmain.Items(0).SubItems(3).ForeColor = Color.Green
Else
lvmain.Items(0).SubItems(3).ForeColor = Color.Red
End If
'Camera
Dim portnum As Integer = 3000
Dim hostname As String = lvmain.Items(0).SubItems(4).Text
Try
Dim client As New TcpClient(hostname, portnum)
Dim ns As NetworkStream = client.GetStream()
lvmain.Items(0).SubItems(4).ForeColor = Color.Green
Catch
lvmain.Items(0).SubItems(4).ForeColor = Color.Red
End Try
End Sub
-
Feb 7th, 2013, 02:52 PM
#6
Re: VS2010 - IP Monitor Program Question
Haven't tested but should do the job ...
vb.net Code:
Try
'Dim sr As StreamReader
'Dim fs As FileStream
'fs = New FileStream(("C:\users\albertc\desktop\offices.txt"), FileMode.OpenOrCreate)
'sr = New StreamReader(fs)
'Dim itm As Object
'itm = sr.ReadLine
'While Not itm = Nothing
For Each itm In IO.File.ReadLines("C:\users\albertc\desktop\offices.txt")
Dim split As String() = itm.Split(New [Char]() {","})
Office = split(0)
Lan = split(1)
Wan1 = split(2)
Wan2 = split(3)
Camera = split(4)
lvMain.Items.Add(Office)
lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Lan)
lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Wan1)
lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Wan2)
lvmain.Items(lvmain.Items.Count - 1).SubItems.Add(Camera)
itm = sr.ReadLine
Next
'sr.Close()
'fs.Close()
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message, "Load Tool Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Feb 7th, 2013, 02:54 PM
#7
Hyperactive Member
Re: VS2010 - IP Monitor Program Question
You can loop throught the Items and SubItems of the ListView control. Instead of the MessageBox, you can ping them and then act accordingly.
Code:
For i As Integer = 0 To lvMain.Items.Count - 1
For j As Integer = 1 To lvMain.Items(i).SubItems.Count - 1
MessageBox.Show(String.Format("{0} : ({1}) {2}", lvMain.Items(i).Text, lvMain.Columns(j).Text, lvMain.Items(i).SubItems(j).Text))
Next
Next
EDIT: The code block messed up my formatting
EDIT 2: Added code to see the Column header related to the sub item.
Last edited by wakawaka; Feb 7th, 2013 at 03:11 PM.
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
|