We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,453 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?

Get Internet Connection State

0
By Jamie on Jan 1st, 2012 9:03 am

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

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

lxXTaCoXxl
Posting Whiz
311 posts since Mar 2011
Reputation Points: 21
Solved Threads: 18
Skill Endorsements: 2

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0607 seconds using 2.69MB