Introduction to My.Computer.Network
September 21st, 2007
My.Computer.Network is another VB.NET My Namespace shortcut class that you can use to handle simple network and Internet operations. In this article we’ll examine the basics of this class with some code examples along the way.
These functions are essentially easy-to-use wrappers for objects found in the System.Net namespace. If you need more than what this class provides then you should look there.
IsAvailable
As you might guess, this property indicates if there is an available network connection or not. The hitch with this function is that it doesn’t tell you much about the connection, it only says if the computer has one or not. For example, a computer might have a local network connection but the Internet connection could be down but this property would return True. Therefore, you should combine this technique with the Ping function to insure that a remote location is available as seen in this code example:
If My.Computer.Network.IsAvailable Then
If My.Computer.Network.Ping("192.168.100.1") Then
My.Computer.Network.UploadFile(LocalFileName, RemoteAddress)
End If
End If
NetworkAvailabilityChanged
This event is raised when the network connectivity changes but, like IsAvailable, it’s not capable of distinguishing between local and remote changes. Therefore, it really isn’t that useful in many situations. It might be handy in a web service client app, for example, a single computer at a remote small store location without a permanent or iffy Internet connection.
This event is built into the MyApplication partial friend class so, if you’re using the Windows Application Framework you just have to add code to the following built-in routine:
Partial Friend Class MyApplication
Private Sub MyApplication_NetworkAvailabilityChanged(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.Devices.NetworkAvailableEventArgs) Handles Me.NetworkAvailabilityChanged
If My.Computer.Network.IsAvailable AndAlso My.Computer.Network.Ping("192.168.100.1") Then
'code to handle/report connection available
Else
'code to handle/report connection down
End If
End Sub
End Class
Ping
As we see in the code examples above Ping does a simple check to see if a remote target computer responds and returns a boolean saying if the check was successful or not. You can use an IP address, like in the examples above, or a DNS resolvable name. This means that for Internet addresses you would not include protocol prefixes like “http://” or “ftp://” but only the name. You can also provide a timeout for the ping in milliseconds and the default is 500ms, a half-second. Increasing this value can be useful if you’re dealing with a slow dial-up connection. Always verify any user input for this call to prevent unwanted exceptions. Note that some servers use firewalls and other methods to protect themselves against ping attacks and the function may return a False when there actually is a system at that address.
Here are some examples of this function:
If My.Computer.Network.Ping("192.168.100.1") Then
...
If My.Computer.Network.Ping("192.168.100.1", 2000) Then
...
If My.Computer.Network.Ping("vbnotebookfor.net") Then
....
If My.Computer.Network.Ping("www.google.com") Then
...
' Will Cause PingException
If My.Computer.Network.Ping("http://www.google.com") Then
DownloadFile and UploadFile
Download, of course, copies a file from a remote location to a local location while Upload does the opposite. Overloads provide several options beyond just the file names or URLs. As with regular file copies the source and target locations have to be accessible or an exception will occur. Therefore, it’s a good idea to do some basic validation before calling either of these routines. Unlike the Ping function, you do need to include a protocol such as “http://” or “ftp://”.
There are overloads for credentials. You can use either clear text username and password parameters (not recommended when you’re transmitting sensitive information!) or, if you’re connecting to a system that supports it, an ICredentials object.
Both have a connection timeout value with a default of 100 seconds. Remember this value is in milliseconds though. As with Ping, this parameter is useful when you have slow conditions.
Both also provide a user interface option where the user will be shown a progress dialog and can cancel the transfer. You will want to set an action to be taken when the user cancels since the default is an exception.
Download also has an option for you to specify if a local file can be overwritten or not. Upload doesn’t offer this so you may get back exceptions if the remote server doesn’t permit the overwrite. It is important to check for exceptions in these methods since there are so many things that can go wrong with remote file transfers.
Here are some simple code examples:
1. Upload file with exception handling, connectivity check, clear text username/password, and showing UI
Try
If My.Computer.Network.IsAvailable AndAlso My.Computer.Network.Ping(RemoteServer) Then
My.Computer.Network.UploadFile(LocalFile, RemoteLocation, UserName, Password, True, 10000)
End If
Catch ex As Exception
'exception handling code here
End Try
2. Download file with exception handling and connectivity check
Try
If My.Computer.Network.IsAvailable AndAlso My.Computer.Network.Ping(RemoteServer) Then
My.Computer.Network.DownloadFile(RemoteLocation, LocalFile)
End If
Catch ex As Exception
'exception handling code here
End Try
3. Upload file with exception handling, connectivity check and the current user’s credentials
Try
If My.Computer.Network.IsAvailable AndAlso My.Computer.Network.Ping(RemoteServer) Then
My.Computer.Network.UploadFile(LocalFile, RemoteLocation, My.User.CurrentPrincipal)
End If
Catch ex As Exception
'exception handling code here
End Try
I hope you’ve found this introductory overview of the My.Computer.Network class useful. If you have anything to add or if you have any questions, please feel free to leave me a comment.
Entry Filed under: VB.NET Tutorials
Rate This Article:











3 Comments Add your own
1. josh | January 15th, 2008 at 11:36 am
great real world description! Much obliged!
2. mary grace | September 23rd, 2008 at 8:06 pm
i would like to ask how you will know or check the progress of downloading and also its completion if you used a progress bar object as a indicator for it?
3. Nicolaas | November 8th, 2008 at 2:32 am
Does this code work in VB Studio .Net 2003.
As I’m new in VB.net, could you provide a complete workable solution/project as in IDE.
Thanks in advance,
Nicolaas
Leave a Comment
Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>
Trackback this post | Subscribe to the comments via RSS Feed