Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Check an internet connection

Check an internet connection

Scheduled Pinned Locked Moved C#
csharphelptutorial
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CodeItWell
    wrote on last edited by
    #1

    How to check that I'm connected on the internet with C#. Help.

    Vasildb

    E G 2 Replies Last reply
    0
    • C CodeItWell

      How to check that I'm connected on the internet with C#. Help.

      Vasildb

      E Offline
      E Offline
      engsrini
      wrote on last edited by
      #2

      Register the event in the class System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged += new System.Net.NetworkInformation.NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged); and in the event handler check for the Internet availability void NetworkChange_NetworkAvailabilityChanged(object sender, System.Net.NetworkInformation.NetworkAvailabilityEventArgs e) { if (!e.IsAvailable) { MessageBox.Show("N/W DISCONNECTED"); } }

      S 1 Reply Last reply
      0
      • E engsrini

        Register the event in the class System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged += new System.Net.NetworkInformation.NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged); and in the event handler check for the Internet availability void NetworkChange_NetworkAvailabilityChanged(object sender, System.Net.NetworkInformation.NetworkAvailabilityEventArgs e) { if (!e.IsAvailable) { MessageBox.Show("N/W DISCONNECTED"); } }

        S Offline
        S Offline
        Scott Dorman
        wrote on last edited by
        #3

        This will work, but it is really only going to show if you are connected to the network. This can be different than being connected and able to hit the internet. You would also need to use a WebRequest or the Ping class and attempt to access a URL that you are reasonably sure will always be available.

        ----------------------------- In just two days, tomorrow will be yesterday.

        1 Reply Last reply
        0
        • C CodeItWell

          How to check that I'm connected on the internet with C#. Help.

          Vasildb

          G Offline
          G Offline
          Glen Harvy 0
          wrote on last edited by
          #4

          #region Required for internet connection - testing! [DllImport("wininet.dll", CharSet = CharSet.Auto)] static extern bool InternetGetConnectedState(ref ConnectionState lpdwFlags, int dwReserved); [Flags] enum ConnectionState : int { INTERNET_CONNECTION_MODEM = 0x1, INTERNET_CONNECTION_LAN = 0x2, INTERNET_CONNECTION_PROXY = 0x4, INTERNET_RAS_INSTALLED = 0x10, INTERNET_CONNECTION_OFFLINE = 0x20, INTERNET_CONNECTION_CONFIGURED = 0x40 } #endregion #region Process - Check internet connection state private void checkInternetConnection() { ConnectionState Description = 0; if (InternetGetConnectedState(ref Description, 0)) { this.tsEmailLabel.Text = "Internet Connection is present."; this.tsEmailLabel.ForeColor = System.Drawing.Color.YellowGreen; } else { this.tsEmailLabel.Text = "Internet Connection is not present."; this.tsEmailLabel.ForeColor = System.Drawing.Color.Crimson; } } #endregion Works for me.

          Glen Harvy

          C 1 Reply Last reply
          0
          • G Glen Harvy 0

            #region Required for internet connection - testing! [DllImport("wininet.dll", CharSet = CharSet.Auto)] static extern bool InternetGetConnectedState(ref ConnectionState lpdwFlags, int dwReserved); [Flags] enum ConnectionState : int { INTERNET_CONNECTION_MODEM = 0x1, INTERNET_CONNECTION_LAN = 0x2, INTERNET_CONNECTION_PROXY = 0x4, INTERNET_RAS_INSTALLED = 0x10, INTERNET_CONNECTION_OFFLINE = 0x20, INTERNET_CONNECTION_CONFIGURED = 0x40 } #endregion #region Process - Check internet connection state private void checkInternetConnection() { ConnectionState Description = 0; if (InternetGetConnectedState(ref Description, 0)) { this.tsEmailLabel.Text = "Internet Connection is present."; this.tsEmailLabel.ForeColor = System.Drawing.Color.YellowGreen; } else { this.tsEmailLabel.Text = "Internet Connection is not present."; this.tsEmailLabel.ForeColor = System.Drawing.Color.Crimson; } } #endregion Works for me.

            Glen Harvy

            C Offline
            C Offline
            CodeItWell
            wrote on last edited by
            #5

            Thanks. That works.

            Vasildb

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups