Get Internet Connection State

lxXTaCoXxl 0 Tallied Votes 232 Views Share

I've developed a code that is intended to be used in an application extension (as with all my codes) that will tell you if the current computer has an internet connection.

To use just include the namespace given at the top of the snippet, then the connection state is a variable of custom type ConnectionState. Usage is for whatever you wish to use it for. If you're using it to display the connection state of the current computer to the user via string (label, textbox, button text, console application) just apply the .ToString() method to the end as with any other variable. It will simply print Connected or Disconnected.

// Required using statement:
using System.Runtime.InteropServices;

public class LAN
{
        [DllImport("wininet.dll")]
        private extern static bool InternetGetConnectedState(out int desc, int reserved);

	public enum ConnectionState
	{
		Connected,
		Disconnected
	}

	public ConnectionState InternetConnection
	{
		get
		{
			ConnectionState Connection = ConnectionState.Disconnected;

			int desc = 0;
			if (InternetGetConnectedState(out desc, 0))
				Connection = ConnectionState.Connected;

			return Connection;
		}
	}
}
lxXTaCoXxl 26 Posting Whiz in Training

I've upgraded this and will be re-posting it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.