Click here to Skip to main content
15,861,168 members
Please Sign up or sign in to vote.
1.10/5 (3 votes)
See more:
check internet connection available or not in c# code
Posted
Updated 27-Mar-18 18:19pm

The simplest solution would be to borrow the function from VB.NET[^]. You can use it after you add a reference to the assembly "Microsoft.VisualBasic.dll".

That would only retrieve the status of the network, and internet might still not be available.

That limits the options to


Enjoy :)
 
Share this answer
 
v2
Comments
[no name] 23-Feb-13 23:10pm    
5!
Ping your server or any site (which remains up always) from your code behind and that will resolve your problem.

Ahhh.... I have a code for that. Though that is in Silverlight, but you can get a bit help from it. Check the WCF service call & implementation to get the idea of it.

Here is the link: How to Ping network IP or Hostname in Silverlight Application?[^]
 
Share this answer
 
Comments
[no name] 23-Feb-13 23:10pm    
5!
Here is the method implementation:

C#
public bool PingNetwork(string hostNameOrAddress)
    {
        bool pingStatus = false;

        using (Ping p = new Ping())
        {
            string data = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
            byte[] buffer = Encoding.ASCII.GetBytes(data);
            int timeout = 120;

            try
            {
                PingReply reply = p.Send(hostNameOrAddress, timeout, buffer);
                pingStatus = (reply.Status == IPStatus.Success);
            }
            catch (Exception)
            {
                pingStatus = false;
            }
        }

        return pingStatus;
    }


Please Vote & "Mark As Answer" if this helps you. Let me know, if you have any issues.
 
Share this answer
 
Comments
jainendra.soft 23-Aug-12 11:06am    
Mark As Answer
You can check for a network connection in .NET 2.0 using
C#
System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()


and for notify use class
C#
System.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged
System.Net.NetworkInformation.NetworkChange.NetworkAddressChanged


NetworkAvailable
 
Share this answer
 
Comments
fjdiewornncalwe 25-Feb-13 14:09pm    
This question is almost 2 years old. Please don't add answers to old questions like this.
C#
public static bool chk_con()
{
    try
    {
        using (var client = new WebClient())
        using (var stream = client.OpenRead("http://www.google.com"))
        {
            return true;
        }
    }
    catch
    {
        return false;
    }
}
 
Share this answer
 
Comments
CHill60 3-Jul-13 8:36am    
The question is 3 years old and already has high-voted solutions. Answering old questions usually only attracts downvoting
using System.Net.NetworkInformation;


C#
private void timer1_Tick(object sender, EventArgs e)
       {
           bool connection = NetworkInterface.GetIsNetworkAvailable();
           if (connection == true)
           {
               MessageBox.Show("available");

           }
           else
           {
           MessageBox.Show("not available");
           }
       }
 
Share this answer
 
Comments
fjdiewornncalwe 25-Feb-13 14:09pm    
This question is almost 2 years old. Please don't add answers to old questions like this.
Member 10813199 12-May-14 14:25pm    
Old or new, it still applies and it solved my problem today in 2014. Thanks for the response Eranga Supun Gamagedara. If you had not responded when you did, this would not have shown up in google search results and I would still be looking for an answer to my question

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900