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.

187 Views
// 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;
		}
	}
}
About the Author
Member Avatar lxXTaCoXxl Posting Whiz in Training

I'm a very difficult person to describe because I'm all over the place. If I could sum me up into one word it would be insane. I'm an incredibly vibrant person. I love surfing, playing guitar, programming, photography, video games, skating, snowboarding, running parkour, hanging out with friends, spending time with my girlfriend, and much more. I'm currently enrolled in college courses and pursuing jobs in my area. Ask if you want to know more.

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