|
Answer» Hello again. I'm working on a project and I need to check if the computer has internet access. The Network.IsAvailable is not good for the project since many computers connect through routers and some routers might not be connected. I tried with the Ping method, if it's connected it WORKS fine, but i'm getting an unhandled EXCEPTION "cannot connect to host" if not connected.
My question is: how can I check if the computer is connected and when it is online to start executing the rest of the code. Also, how can I disable unhandled exceptions? If an exception occurs then the program continues executing without ERROR or "not responding".
Thank you!use Ping. The way to "disable" unhandled exceptions is to handle them.Another option: since ping only really tests if a machine is responding and doesn't ALWAYS correspond with the availability of HTTP requests, we can do the same thing that win Vista/7 do to check the network connection:
Code: [Select]public bool ConnectionAvailable() { try { var result = new WebClient().DownloadString("http://www.msftncsi.com/ncsi.txt"); return true;
} CATCH (Exception exx) { return false; }
}
Which I think would translate to something like the following for VB.NET:
Code: [Select]Public Function ConnectionAvailable() As Boolean Try Dim Result As String = (new WebClient).DownloadString("http://msftncsi.com/ncsi.txt") return true Catch exx As Exception return false End Try End Function
|