vb.net Tips & Tricks

Need a snippet of vb.net code to perform a particular programming task? Most coders (like the author of this Blog) don't keep every coding trick they've learned stored in their head - they figure out a solution once and refer back to previous programs when they need to use it again. Check here for (hopefully) useful chunks of code to reduce your programming efforts.

Is vb.net too limited for you?
Need a scripting environment that won't hold you back?

If so, move up to Windows PowerShell and check out some useful tips here: http://poshtips.com

Ping A System from vb.Net

Network ping is a quick way to check to see if a remote system is online (assuming the remote system does not have ping response blocked). There are basically two steps involved in this example: First, make sure that we are able to resolve the hostname to an IP address; if we can't do that, there's no point in attempting to ping it. Once we are able to resolve the hostname's IP address, we can move on to second step which is the actual ping operation. This is relatively simple in .Net Framework (2.0) using vb.Net.

Environment: VB.NET, Visual Studio 2005, .NET 2.0

STEP ONE:
Create a Windows Form application and place the following three controls on it.

(1) A TextBox control
- change the name to "txtHostName"
(2) A Button conrol
- change the name to "btnPingHost"
(3) A second TextBox Control
- change the name to "txtPingResult"
- set the "Multiline" property to "True"
- make the control big enough to show the ping output,
approximately 400(width) by 200(height)


STEP TWO:
Paste the following code into the "codebehind" for this form.



Imports System.Net.NetworkInformation
Imports system.net
Imports System.Threading
Imports System.Text

Public Class Form1

Private Sub btnPingHost_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPingHost.Click
If HostIsResolvable(txtHostName.Text) Then
PingSystem(txtHostName.Text)
End If
End Sub

Sub PingSystem(ByVal HostName As String)
'Ping the system and display results to the txtPingResult control

'Clear any previous ping results
txtPingResult.Text = ""

Dim pingsender As New Ping
Dim options As New PingOptions

'Modify the default fragmentation behavior
options.DontFragment = True

'Create a buffer of 32 bytes of data to be transmitted.
Dim data As String = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
Dim buffer As Byte() = Encoding.ASCII.GetBytes(data)
Dim timeout As Integer = 120

Try
'Attempt ping
Dim reply As PingReply = pingsender.Send(HostName, timeout, buffer, options)
'Ping the target system 4 times to check for consistent results
For i As Integer = 1 To 4
If (reply.Status = IPStatus.Success) Then
txtPingResult.Text += String.Format("Reply from:{1}: Bytes={2} time{3} TTL={4}{0}", _
vbCrLf, reply.Address.ToString, reply.Buffer.Length, GetMs(reply.RoundtripTime), reply.Options.Ttl)
Else
txtPingResult.Text += String.Format("Ping Failed:{1}{0}", vbCrLf, reply.Status.ToString)
End If
Me.Refresh()
'Pause for 1 second before pinging again
System.Threading.Thread.Sleep(1000)
Next
Catch ex As Exception
txtPingResult.Text += String.Format("Ping Failed on host:{1}{0}Error:{2}{0}Detail:{3}{0}", vbCrLf, HostName, ex.Message, ex.InnerException)
End Try
txtPingResult.Text += "-- done --"
End Sub

Function HostIsResolvable(ByVal HostName As String) As Boolean
'Attempt to lookup hostname in DNS
'If successful, simply return TRUE
'If not successful, Return FALSE and display message to txPingResult control
Try
'performs the DNS lookup
Dim HostEntry As IPHostEntry = Dns.GetHostEntry(HostName)
Dim IpList As IPAddress() = HostEntry.AddressList
HostIsResolvable = True
Catch ex As System.Exception
txtPingResult.Text = ex.Message
HostIsResolvable = False
End Try

End Function

Function GetMs(ByVal ResponseTime As Integer) As String
'accept and integer value and return a friendly string
' reflecting number of milliseconds
If ResponseTime = 0 Then
Return "<1ms">
Else
Return String.Format("={0}ms", ResponseTime.ToString)
End If
End Function


No comments: