Checking an Internet Connection in C#

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: design 3. Now put the System.Runtime.InteropServices namespace after System.Windows.Forms; This will trigger to access the Internet Operation Services of the computer.
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Runtime.InteropServices;
4. Import the DLL library of wininet.dll. Put this code after the bracket of InitializeComponent();.
  1.         public Form1()
  2.         {
  3.             InitializeComponent();
  4.         }
  5.        [DllImport("wininet.dll")]
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.
  1.  private extern static bool InternetGetConnectedState(out int des, int value);
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.
  1.  bool IsConnectedToInternet()
  2.        {
  3.            bool check;
  4.            int description;
  5.            check = InternetGetConnectedState(out description, 0);
  6.            return check;
  7.        }
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.
  1.         private void button1_Click(object sender, EventArgs e)
  2.         {
  3.             if (IsConnectedToInternet() == true)
  4.             {
  5.                 MessageBox.Show("There is an Internet Connection.", "Check Internet Connection");
  6.             }
  7.             else
  8.             {
  9.                 MessageBox.Show("There is No Internet Connection.", "Check Internet Connection");
  10.             }
  11.         }
Press "F5" to run it and Click the button. Output: 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

Add new comment