Get Information About a Network Connection

Some applications need to adjust how they work based on whether a network connection is present. For example, imagine a sales reporting tool that runs on the laptop of a traveling sales manager. When the laptop is plugged into the network, the application needs to run in a connected mode in order to retrieve the information it needs, such as a list of products, directly from a database or web service. When the laptop is disconnected from the network, the application needs to gracefully degrade to a disconnected mode that disables certain features or falls back on slightly older data that's stored in a local file. To make the decision about which mode to use, an application needs a quick way to determine the network status of the current computer. Thanks to the new My.Computer.Network object, this task is easy.

Note

Need to find out if your computer's currently online? With the My class, this test is just a simple property away.

How do I do that?

The My.Computer.Network object provides a single IsAvailable property that allows you to determine if the current computer has a network connection. The IsAvailable property returns True as long as at least one of the configured network interfaces is connected, and it serves as a quick-and-dirty test to see if the computer is online. To try it out, enter the following code in a console application:

If My.Computer.Network.IsAvailable Then Console.WriteLine("You have a network interface.") End If ...

Get Visual Basic 2005: A Developer's Notebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.