Check an internet connection
-
How to check that I'm connected on the internet with C#. Help.
Vasildb
-
How to check that I'm connected on the internet with C#. Help.
Vasildb
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 availabilityvoid NetworkChange_NetworkAvailabilityChanged(object sender, System.Net.NetworkInformation.NetworkAvailabilityEventArgs e) { if (!e.IsAvailable) { MessageBox.Show("N/W DISCONNECTED"); } }
-
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 availabilityvoid NetworkChange_NetworkAvailabilityChanged(object sender, System.Net.NetworkInformation.NetworkAvailabilityEventArgs e) { if (!e.IsAvailable) { MessageBox.Show("N/W DISCONNECTED"); } }
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.
-
How to check that I'm connected on the internet with C#. Help.
Vasildb
#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
-
#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
Thanks. That works.
Vasildb