{"id":18,"date":"2008-10-29T05:38:06","date_gmt":"2008-10-29T00:38:06","guid":{"rendered":"http:\/\/zubair.info\/blog\/?p=18"},"modified":"2019-08-13T19:37:23","modified_gmt":"2019-08-13T14:37:23","slug":"vbnet-how-to-check-if-you-are-connected-to-internet-using-vb-2005","status":"publish","type":"post","link":"https:\/\/www.zubair.info\/blog\/2008\/10\/29\/vbnet-how-to-check-if-you-are-connected-to-internet-using-vb-2005.html","title":{"rendered":"VB.net &#8211; How to check if you are connected to internet using VB 2005"},"content":{"rendered":"<p>Determining if you or your application is connected to the internet may sound like an easy task &#8211; But it really wasn&#8217;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\u2026 In my search results, I have only seen people praising to use the <strong>wininet.dll<\/strong> API, I do not know why people even bothered to post useless code like that, it really does not work! <!--more--> However, I think I may be wrong &#8211; people with different types of internet connection might see different results, so it is completely up to you &#8211; If you have not tried that out already you should do so now. You never know, maybe it might help in your situation \u2013 but from what I have been hearing that the <strong>wininet.dll<\/strong> API is not that reliable. <strong>The two working solutions I found so far:<\/strong> The <strong>first method<\/strong> is to ping a host (which is the most common): <\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\"> Function IsConnected() As Boolean Return My.Computer.Network.Ping(&quot;72.14.207.99&quot;) End Function <\/pre>\n<p> I recommend using IP Address (127.0.0.1) instead of a hostname (like <a href=\"http:\/\/www.google.com\">www.google.com<\/a>). When you do use this method you have to be all the time \u2013 for instance (www.google.com) or (<a title=\"Official Website for Microsoft\" href=\"http:\/\/www.microsoft.com\">www.microsoft.com<\/a>) is very less likely to go down. The <strong>second method<\/strong> is my personal, which I thought was more reliable atleast in my case. <\/p>\n<pre class=\"brush: vb; title: ; notranslate\" title=\"\"> Function IsConnected() As Boolean Try My.Computer.Network.DownloadFile(&quot;http:\/\/www.google.com\/abc.txt&quot;, &quot;app_tool.dll&quot;, &quot;&quot;, &quot;&quot;, True, 100, True) If My.Computer.FileSystem.GetFileInfo(&quot;app_tool.dll&quot;).Length &gt; 1 Then Return True End If Catch Return False End Try End Function <\/pre>\n<p> This function downloads a file from a reliable source that is always online like (<a href=\"http:\/\/www.google.com\">www.google.com<\/a>) and (<a href=\"http:\/\/www.microsoft.com\">www.microsoft.com<\/a>) \u2013 if the download is successful we go further and check the length. We use <strong>My.Computer.Network.DownloadFile<\/strong> 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 <strong>&#8220;&#8221;<\/strong> 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\u2019t work quite correctly but I have been seeing people post messages alot that it is the best method \u2013 which is using a <strong>System.Net.WebRequest<\/strong>, But before you go let me also tell you that this method is also alot slower than the above.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Determining if you or your application is connected to the internet may sound like an easy task &#8211; But it really wasn&#8217;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 \/ [&hellip;]<\/p>\n","protected":false},"author":1001009,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[10,43,7,42,17,19,16,75,74,9,94],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-visual-basic","tag-net-framework","tag-connection","tag-microsoft","tag-network","tag-programming","tag-snippets","tag-vb","tag-vb-functions","tag-vb-snippets","tag-vbnet","tag-visual-basic"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.zubair.info\/blog\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zubair.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zubair.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zubair.info\/blog\/wp-json\/wp\/v2\/users\/1001009"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zubair.info\/blog\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":0,"href":"https:\/\/www.zubair.info\/blog\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zubair.info\/blog\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zubair.info\/blog\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zubair.info\/blog\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}