Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ping in VB .NET

Status
Not open for further replies.

Shift838

IS-IT--Management
Jan 27, 2003
987
US
I have been attempting to get a ping to work within VB .Net with error checking.

All the code I have tried and found works great as long as the computer is online. It fails if the computer is offline. Does anyone have any code to Ping a remote host with code that will work if the computer is off line and not thrown an exception error?

I haven been using the system.net.networkinformation name space to utilize the Ping command there. but to no avail.
 
How about something like:
Code:
If My.Computer.Network.Ping("Address") Then
            MsgBox("Server pinged successfully.")
        Else
            MsgBox("Ping request timed out.")
        End If
Originally taken from here

Hope this helps

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
tried this code before and it gives me the exception error if the computer is offline.
 
Why do you not want an Exception? Just let the exception occur and handle it.

Code:
        Try
            My.Computer.Network.Ping("SomeComputer")
        Catch ex As Exception
            MessageBox.Show("cannot ping")
        End Try
 
RiverGuy,

I tested your above code and got it working on a test button but I am having issue with incorporating it with my code.

Test code working was:

Dim servers As Object
For Each servers In lstServers.Items

Try
My.Computer.Network.Ping(servers)
FileOpen(1, "c:\temp\test.csv", OpenMode.Append)
Print(1, servers, ",OK")
Print(1, vbCr)
FileClose(1)
Catch ex As Exception
FileOpen(1, "c:\temp\test.csv", OpenMode.Append)
Print(1, servers, ",unreachable")
Print(1, vbCr)
FileClose(1)
End Try

Next
MsgBox("COMPLETE")


Code not working: what I have noticed is that I cannot place the Next in the code that is not working outside of the End Try, this causes it to stop when it hits a server that does not respond.

Private Sub cmdGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGo.Click
Dim serveritems As Object
If File.Exists(txtCSVFile.Text) Then
MsgBox("found")
Else
MsgBox("Creating CSV file")
FileOpen(1, txtCSVFile.Text, OpenMode.Output)
Print(1, "Server Name,Status,")
Dim airid As String
For Each airid In lstairid.Items
Print(1, airid & ",")
Next
Print(1, vbCr)
FileClose(1)
End If

Dim MyRegKey, MyRegKey2 As Microsoft.Win32.RegistryKey
Dim MyVal, MyVal2
lblstart.Visible = True
Dim imp As New RunAs_Impersonator
Try
imp.ImpersonateStart(cmbDomain.SelectedValue, txtUserID.Text, txtpword.Text)


Dim SERVERFILE_NAME, AIRIDFILE_NAME, CSVFILE_NAME As String
SERVERFILE_NAME = txtServerFile.Text
AIRIDFILE_NAME = txtairidfile.Text
CSVFILE_NAME = txtCSVFile.Text

Dim st As Integer
Dim tt As Integer
tt = lstServers.Items.Count * lstairid.Items.Count
'Dim serveritems As Object

For Each serveritems In lstServers.Items
st = st + 1
FileOpen(1, txtCSVFile.Text, OpenMode.Append)
'MsgBox(serveritems)
My.Computer.Network.Ping(serveritems)
Print(1, serveritems & ", OK,")
FileClose(1)
lblServerTotal.Text = st & "/" & lstServers.Items.Count

Dim MyReg As Microsoft.Win32.RegistryKey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, serveritems)
MyRegKey = MyReg.OpenSubKey("Software\Microsoft\Windows NT\currentVersion")
MyVal = MyRegKey.GetValue("ProductName")
'MsgBox(MyVal)

If MyVal = "Microsoft Windows Server 2003" Then
Dim aid As Object
For Each aid In lstairid.Items
Dim Myreg2 As Microsoft.Win32.RegistryKey = Microsoft.Win32.RegistryKey.OpenRemoteBaseKey(Microsoft.Win32.RegistryHive.LocalMachine, serveritems)
MyRegKey2 = Myreg2.OpenSubKey("SOFTWARE\Chevron\GIL\Setup\" & aid)

If MyRegKey2 Is Nothing Then
FileOpen(1, txtCSVFile.Text, OpenMode.Append)
Print(1, "Unsuccessful,")
FileClose(1)
Else
MyVal2 = MyRegKey2.GetValue("Status")
If MyVal2 = "Installed" Then
FileOpen(1, txtCSVFile.Text, OpenMode.Append)
Print(1, "Successful,")
FileClose(1)
Else
FileOpen(1, txtCSVFile.Text, OpenMode.Append)
Print(1, "Unsuccessful,")
FileClose(1)
End If
End If
Next
FileClose(1)
End If
FileOpen(1, txtCSVFile.Text, OpenMode.Append)
Print(1, vbCr)
FileClose(1)
Next
Catch ex As Exception

Print(1, ",,unreachable")
Print(1, vbCr)
FileClose(1)

End Try

FileClose(1)
MsgBox("Complete")
lblstart.BackColor = Color.DarkSeaGreen
lblstart.Text = "Complete"
End Sub

 
Have you thought about writing your own FUNCTION? All that code in the click event? Oh man. I've fired people for less.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
Good idea, but I've never written a function and I'm new to VB .NET
 
Functions are amazing things. You can write one function and then call it from other functions (even click events). In my opinion, there should be very little code in a click event because it's very hard to re-use. Writing your own function is super easy. All you need to do is add some code outside of a click event.

For example.... after your end sub line, press the enter key on your keyboard a couple times. This will create a couple blank lines for you. You can then write your own function in those blank lines (realizing that they won't be blank after you type stuff on to them).

You could write a function like this...

Code:
Private Function CanPingServer(ByVal ServerName As String) As Boolean

  Try
    If My.Computer.Network.Ping(ServerName) Then
      Return True
    Else
      Return False
    End If
  Catch ex As Exception
    Return False
  End Try

End Function

Then, wherever you want to determine if you can ping a server, you can simply call this function, which will return a boolean (True/False).

Congratulations! You just wrote your first bit of re-usable code.

Good luck.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
This function is good. But how can I transfer like a

For each servernames in lstservers.items

So the ping will ping each servername within the lstbox?
 
[/code]
Dim servers As Object
For Each servers In lstServers.Items

If CanPingServer(servers) Then
My.Computer.Network.Ping(servers)
FileOpen(1, "c:\temp\test.csv", OpenMode.Append)
Print(1, servers, ",OK")
Print(1, vbCr)
FileClose(1)
Else
FileOpen(1, "c:\temp\test.csv", OpenMode.Append)
Print(1, servers, ",unreachable")
Print(1, vbCr)
FileClose(1)
End If

Next
MsgBox("COMPLETE")
[/code]

Since the CanPingServer function has proper error handling, you don't need to worry about having error handling in the calling function.

-George

"The great things about standards is that there are so many to choose from." - Fortune Cookie Wisdom
 
This function would be added to a module or a vb class?
 
I got it working. Thanks for the help. Now i just need to get everything else working..
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top