Support Forums

Full Version: [TuToRiAl] How to Check if theres any Internet Connection [TuToRiAl]
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3
Here is the code in C# (if anyone needs it)
Code:
using System;
using System.Net;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class Form1
    {
        public Form1()
        {
            InitializeComponent();

            //Added to support default instance behavour in C#
            if (_defaultInstance == null)
                _defaultInstance = this;
        }

        #region Default Instance

        private static Form1 _defaultInstance;

        /// <summary>
        /// Added by the VB.Net to C# Converter to support default instance behavour in C#
        /// </summary>
        public static Form1 Default
        {
            get
            {
                if (_defaultInstance == null)
                {
                    _defaultInstance = new Form1();
                    _defaultInstance.FormClosed += DefaultInstanceFormClosed;
                }

                return _defaultInstance;
            }
        }

        private static void DefaultInstanceFormClosed(object sender, FormClosedEventArgs e)
        {
            _defaultInstance = null;
        }

        #endregion

        public void Form1Load(Object sender, EventArgs e)
        {
            if (IsConnectionAvailibe())
                //If there was internet on at the moment the application opened, add statements to enable the application for internet usage.
            {
                Label1.Visible = false;
                TextBox2.Enabled = true;
                TextBox1.Enabled = true;
                Button1.Enabled = true;
                Button2.Enabled = true;
                Button3.Enabled = true;
            }
            else if (IsConnectionAvailibe() == false)
                //If there wasn't internet when the application opened, add statements to disable internet using items to prevent errors.
            {
                Label1.Visible = true;
                TextBox2.Enabled = false;
                TextBox1.Enabled = false;
                Button1.Enabled = false;
                Button2.Enabled = false;
                Button3.Enabled = false;
            }
        }

        public bool IsConnectionAvailibe()
        {
            var objUrl = new Uri("http://www.google.com");

            var objWebRequest = WebRequest.Create(objUrl);
            WebResponse objResp;

            try
            {
                objResp = objWebRequest.GetResponse();
                if (objResp != null) objResp.Close();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
    }
}

Or you can use this :

[Image: jlezmfpib9m8splzbu9p.png]

Here is the upgraded source to vb 2008 in vb net and C#
Source in vb net & C#

Like people have already said, this could be done much simpler with either a webclient downloading string, or even a ping. But nevertheless this is still working and a nice and handy piece of code ;)
Good job @ OP
Pages: 1 2 3