Checking an Internet Connection in C#
Submitted by donbermoy on Tuesday, May 27, 2014 - 11:54.
This is a tutorial in which we will going to create a program that can determine if your are connected in the internet or not using C#.
Now, let's start this tutorial!
1. Let's start with creating a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application. Name your program internet csharp.
2. Next, add only one Button named Button1 and labeled it as "Check Internet Connection". You must design your interface like this:
3. Now put the System.Runtime.InteropServices namespace after System.Windows.Forms; This will trigger to access the Internet Operation Services of the computer.
4. Import the DLL library of wininet.dll. Put this code after the bracket of InitializeComponent();.
5. Create a Boolean variable that is extern static with the function named of InternetGetConnectedState with description and value as parameters. This InternetGetConnectedState is in the library of wininet.dll. Meaning, you cannot modify this function.
6. Create a method named IsConnectedToInternet() as Boolean. We will use our function InternetGetConnectedState into this method. This will trigger to determine and prompt if your computer is connected to internet or not.
7. Now, for the last method, put this code for your btnCheck. If IsConnectedToInternet() method will return True, then you are connected in the internet. Otherwise, not.
Press "F5" to run it and Click the button.
Output:
Best Regards,
Engr. Lyndon Bermoy
IT Instructor/System Developer/Android Developer/Freelance Programmer
If you have some queries, feel free to contact the number or e-mail below.
Mobile: 09488225971
Landline: 826-9296
E-mail:[email protected]
Add and Follow me on Facebook: https://www.facebook.com/donzzsky
Visit and like my page on Facebook at: https://www.facebook.com/BermzISware

- 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;
- public Form1()
- {
- InitializeComponent();
- }
- [DllImport("wininet.dll")]
- private extern static bool InternetGetConnectedState(out int des, int value);
- bool IsConnectedToInternet()
- {
- bool check;
- int description;
- check = InternetGetConnectedState(out description, 0);
- return check;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- if (IsConnectedToInternet() == true)
- {
- MessageBox.Show("There is an Internet Connection.", "Check Internet Connection");
- }
- else
- {
- MessageBox.Show("There is No Internet Connection.", "Check Internet Connection");
- }
- }

Add new comment
- 376 views