Results 1 to 7 of 7

Thread: VS2010 - IP Monitor Program Question

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    12

    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

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    12

    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

  4. #4
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  5. #5

    Thread Starter
    New Member
    Join Date
    Jul 2011
    Posts
    12

    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

  6. #6
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: VS2010 - IP Monitor Program Question

    Haven't tested but should do the job ...

    vb.net Code:
    1. Try
    2.  
    3.             'Dim sr As StreamReader
    4.             'Dim fs As FileStream
    5.             'fs = New FileStream(("C:\users\albertc\desktop\offices.txt"), FileMode.OpenOrCreate)
    6.             'sr = New StreamReader(fs)
    7.  
    8.             'Dim itm As Object
    9.             'itm = sr.ReadLine
    10.             'While Not itm = Nothing
    11.             For Each itm In IO.File.ReadLines("C:\users\albertc\desktop\offices.txt")
    12.                 Dim split As String() = itm.Split(New [Char]() {","})
    13.                 Office = split(0)
    14.                 Lan = split(1)
    15.                 Wan1 = split(2)
    16.                 Wan2 = split(3)
    17.                 Camera = split(4)
    18.  
    19.                 lvMain.Items.Add(Office)
    20.                 lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Lan)
    21.                 lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Wan1)
    22.                 lvMain.Items(lvMain.Items.Count - 1).SubItems.Add(Wan2)
    23.                 lvmain.Items(lvmain.Items.Count - 1).SubItems.Add(Camera)
    24.                 itm = sr.ReadLine
    25.             Next
    26.  
    27.             'sr.Close()
    28.             'fs.Close()
    29.         Catch ex As System.Exception
    30.  
    31.             System.Windows.Forms.MessageBox.Show(ex.Message, "Load Tool Data Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    32.  
    33.         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!

  7. #7
    Hyperactive Member
    Join Date
    Jan 2010
    Posts
    259

    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
  •  



Click Here to Expand Forum to Full Width