Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TuToRiAl] How to Check if theres any Internet Connection [TuToRiAl]
#21
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#

Reply
#22
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [HELP]Tutorial/Learning System[HELP] norBATman 2 1,327 09-08-2012, 10:17 PM
Last Post: AceInfinity
  [TuToRiAl] How to make Offline (No Internet) Captcha (Human Confirmation) [TuToRiAl] Resistance 24 12,674 08-30-2012, 10:14 PM
Last Post: Resistance
  [Visual Basic] FTP Uploader [Tutorial] Coding Support 6 3,042 08-12-2012, 12:36 AM
Last Post: Kenneth
  [RELEASE]SF-TutPad "Tutorial Editor" [RELEASE] Digital-Punk 28 10,308 08-11-2012, 11:25 PM
Last Post: Kenneth
  Emailer Tutorial dunlop03 12 2,900 06-24-2012, 04:33 AM
Last Post: kpn37z

Forum Jump:


Users browsing this thread: 3 Guest(s)