Check for Internet Connection (Final Part)

Category: .NET Framework

Check for an Internet Connection – Part Three of Three

This is the Final Part of this article series. Read Part 2 here


In the previous two parts of this series of articles we have shown how we can utilize an external library and import that library into our code very easily.  We also showed how we call that external library and display the results of the call to the external library.
Has started to design the application and have created the enumeration, screen and methods necessary to check for an Internet connection.

We migrated our web sites to Server Intellect over one weekend and the setup was so smooth that we were up and running right away. They assisted us with everything we needed to do for all of our applications. With Server Intellect's help, we were able to avoid any headaches!


In this example we will show you entire set of source code and what the finished application looks like.  You can take this source code and cut and paste it into Visual Studio, or you can download the attached compressed file and open the project within Visual Studio.

Source Code

We are now ready to see how everything is put together and how we trigger the call for the check of an Internet connection.  The following source code can be cut and pasted into Visual Studio for your use or you can open the project in the attached compressed file that holds the source code.



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace CheckInternet
public partial class Form1 : Form
public enum StateOfConnection
Modem = 0x1,
Lan = 0x2,
Proxy = 0x4,
Installed = 0x10,
OffLine = 0x20,
Configured = 0x40
[DllImport("wininet.dll", CharSet = CharSet.Auto)]
static extern bool InternetGetConnectedState(ref StateOfConnection lpdwFlags, int dwReserved);
public Form1()
private void btnTestConnection_Click(object sender, EventArgs e)
StateOfConnection MyState = 0;
txtFlag.Text = InternetGetConnectedState(ref MyState, 0).ToString();
txtState.Text = MyState.ToString();

We moved our web sites to Server Intellect and have found them to be incredibly professional. Their setup is very easy and we were up and running in no time.

Compile and execute the above code.  When the application starts you will be presented with a screen that will allow you to select a button to “Test” the Internet connection.  The results of that connection test will be displayed within two labels.  Please see Fig. 2 for a visual representation of the screen we just discussed.
Screen shot of the results of the above example: Fig. 2

[Click to see full-size]

What have we learned?

Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!

  • That C# has the ability to import external libraries easily
  • That we can test an Internet Connection before we try to access it
  • How to use an enumeration
  • That an enumeration can be treated as a bit field
  • Definition of Connection States
  • How we call and utilize the external library

This is the Final Part of this article series. Read Part 1 here


Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!

100% SPAM FREE! We will never sell or rent your email address!