How To Do Credit Card Validation in VB.NETVB.NET Interview Questions #1

Introduction to My.Computer.Network

September 21st, 2007

My.Computer.Network: An Internet ShortcutMy.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.


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("") Then
        My.Computer.Network.UploadFile(LocalFileName, RemoteAddress)
    End If
End If


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("") Then
            'code to handle/report connection available
            'code to handle/report connection down
        End If
    End Sub
End Class


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("") Then


If My.Computer.Network.Ping("", 2000) Then


If My.Computer.Network.Ping("") Then


If My.Computer.Network.Ping("") Then


' Will Cause PingException
If My.Computer.Network.Ping("") 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

    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

    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

    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.

Share This Article: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • Reddit
  • StumbleUpon
  • Technorati
  • DotNetKicks
  • DZone

Entry Filed under: VB.NET Tutorials

Rate This Article:

Not That GoodCould Be BetterOKGoodGreat (3 votes, average: 3.33 out of 5)
Loading ... Loading ...

8 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, could you provide a complete workable solution/project as in IDE.

    Thanks in advance,

  • 4. MOHAMMED  |  March 5th, 2009 at 9:50 am

    How to display transfer rate for upload/download files?

    Please send me code (VB.NET 2005)
    Please reply me

  • 5. Chris McKirgan  |  June 7th, 2009 at 3:15 pm

    Brilliant, of everything on the web I found this the only helpful one, and very fluent and helpful indeed. Thank you so much for taking th time to write this. Its appreciated so much!

  • 6. emmaddai  |  July 5th, 2009 at 4:33 pm

    Good codes.
    the downloadFile works for me alright, but the upload file always gives me error.
    can the upload file work with php form instead of as file?

  • 7. Glenn Nassar  |  October 16th, 2009 at 1:17 pm

    How do you trap a username or password error without it being directed to windows error reporting?

  • 8. Glenn Nassar  |  October 16th, 2009 at 1:22 pm

    I should have mention I am downling from a FTP Server


    My.Computer.Network.DownloadFile(strUpdaterAddress + “Version.txt”, _
    _xmlPath & “temp\version.txt”, strUpdaterUser, strUpdaterPassword, False, 4000, True, FileIO.UICancelOption.DoNothing)

    Catch ex As System.Net.WebException


    End Try

    The following catches the error but tries to report it to microsoft. I wish to turn that portion off programatically, Not through the registry or system advanced settings.
    Thank You

Leave a Comment


Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed

Visit Me At My New Site, Programming In C#

Most Popular Articles

Highest Rated Articles


Most Recent Articles


 Subscribe in a reader

To subscribe by e-mail
Enter your address here

Delivered by FeedBurner

VB Opportunities