≡ Menu

VB.net – How to check if you are connected to internet using VB 2005

Determining if you or your application is connected to the internet may sound like an easy task – But it really wasn’t as easy as it sounds for me when i was trying to figure a way out to find a way to know if my computer is connected to the internet using VB.net / Visual Basic 2005. I have done quite a search on this… In my search results, I have only seen people praising to use the wininet.dll API, I do not know why people even bothered to post useless code like that, it really does not work! However, I think I may be wrong – people with different types of internet connection might see different results, so it is completely up to you – If you have not tried that out already you should do so now. You never know, maybe it might help in your situation – but from what I have been hearing that the wininet.dll API is not that reliable. The two working solutions I found so far: The first method is to ping a host (which is the most common):

 Function IsConnected() As Boolean Return My.Computer.Network.Ping("72.14.207.99") End Function 

I recommend using IP Address (127.0.0.1) instead of a hostname (like www.google.com). When you do use this method you have to be all the time – for instance (www.google.com) or (www.microsoft.com) is very less likely to go down. The second method is my personal, which I thought was more reliable atleast in my case.

 Function IsConnected() As Boolean Try My.Computer.Network.DownloadFile("http://www.google.com/abc.txt", "app_tool.dll", "", "", True, 100, True) If My.Computer.FileSystem.GetFileInfo("app_tool.dll").Length > 1 Then Return True End If Catch Return False End Try End Function 

This function downloads a file from a reliable source that is always online like (www.google.com) and (www.microsoft.com) – if the download is successful we go further and check the length. We use My.Computer.Network.DownloadFile to download the file, we pass some special parameters to it like the complete URL to the file we want to download and then the filename we want to give it after its copied to our hard-drive. We can also pass values such as a username and password (I have these set to an empty string like this “” with two double quotes). One of the useful parameter is if or not we want to show the end user a UI (User Interface) showing that a file is being downloaded to their computer, This is a boolean value True or False. Then there is the second last parameter Timeout which takes an integer as a value (Timeout is set to 100 seconds by default) and last but not least is the overwrite parameter which is to tell weather or not we want to overwrite the file, we can set that to either true or false. There is another method but I wont be discussing about that since it didn’t work quite correctly but I have been seeing people post messages alot that it is the best method – which is using a System.Net.WebRequest, But before you go let me also tell you that this method is also alot slower than the above.

Share
{ 10 comments… add one }
  • Kris Morrison November 18, 2009, 7:09 am

    You Could Try This

    Public Function CheckNetwork() As Boolean
    Return My.Computer.Network.IsAvailable
    End Function

  • Jace June 14, 2010, 5:15 am

    The .Network.IsAvailable() function is not reliable. It will return True if windows is connected to a network – be it internet or Lan..

    The second method is the best available option.

  • Alice June 14, 2010, 9:47 am

    I completely agree with the above comment!

    .Network.IsAvailable() function is not reliable at all, If you are thinking of using that be prepared for alot of troubleshooting with your app.

    ^_^

  • Eric August 26, 2010, 2:44 pm

    The second option works fine for one small thing.

    You will need to make sure the file you want to download (in the example abc.txt) is present because if it isn’t you’ll get an exception error for file not found.
    So then you are connected to the internet, but the function still returns false.

    I used http://www.google.com/index.html and it works great.

    thanks!

  • Joacim Andersson [MVP] December 22, 2011, 4:03 pm

    I do not agree with many things that has been said in this post. First of all, the author recommends using IP addresses rather than host names, IP addresses might very well change while it’s unlikely that Google for example will ever change their host name.

    Using Ping might fail for a number of reasons, it might be blocked by a firewall or router, or the Ping IP Port might simply be turned off on the target computer.

    Downloading a file can also fail for a bunch of reasons. First of all, what guarantees that the file will exist in the future? Even the index.html file on Google might very well get a new name in the future. Secondly you have to ensure that you have permission to create a file in the destination folder. In the above example that folder would be the current path which could be anywhere but most likely the folder from where your application is launched from which probably is in the users Program Files folder, which you don’t have permission to write to if UAC is active.

    The method I normally use is to check if a host name simply can be resolved. If you can’t resolve a host name such as http://www.google.com then you probably aren’t online. Of course this approach isn’t 100% sure to succeed either since Google might disappear (but that that would happen sometime in the near future is very unlikely.

      Private Function IsOnline() As Boolean
        Try
          Dim dummy As IPHostEntry = Dns.GetHostEntry("www.google.com")
          Return True
        Catch ex As SocketException
          Return False
        End Try
      End Function
    
  • Paul June 3, 2012, 5:44 pm

    Joacim Andersson is absolutely right!

  • Imdadul June 3, 2012, 9:28 pm

    You r absolutely right….:) Joacim Andersson [MVP] …..Thank you

  • Zubair June 3, 2012, 9:50 pm

    Hey Guys, thanks for your comments and opinions.
    This is a really old post and i haven’t been getting any time to update my post.

    @Joacim Andersson [MVP]:
    The reason i recommended using IP is because its faster that way. Resolving a ‘Host Name’ takes longer and could not give you a valid response all the time either. I know because on my PC it doesn’t resolve Google.com. But that doesn’t mean i’m Not Connected. i’m still connected and google.com is loading perfectly fine in my web browser. I haven’t gone down or troubleshooted as to why the domain isn’t resolving but i have a hunch its due to my ISP setting some sort of a limitation since its not just google.com, i can’t resolve any domains. But since this is might be an ISP related issue others might experience the same issue and using only the IP’s instead of ‘Host Name’ might be the only solution.

    But the main reason i chose to use IP’s was because that was alittle faster for me. But i agree that using a hostname is preferred over IP only because the IP’s might change in the future as you stated and i have switched to using host names myself for quite a while .

    Thanks again for your comments.

  • khadija baha May 28, 2013, 8:44 pm

    tnks a lot Joacim Andersson [MVP] it is working tnks a lot friends

  • Fuzznutz October 19, 2014, 11:14 pm

    Bravo to Mr. Andersson! I’m a mere lamer and had the same thoughts he posted. While I most appreciate & am very grateful that everyone shares information I am puzzled as to why ‘we’ don’t know answers to what seems to be simple problems. Somebody wrote dot net, assembler, C# and a gazillion other programming languages even entire operating systems & I can’t even check to see if I’m connected to the f&^:’en internet. sheeze

    Anyway, thanks for allowing me to have access to a wealth of information…….

Leave a Comment