Start New Discussion within our Software Development Community

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;
		}
	}
}