s

wpf function to check if internet connection is available edit button Edit

author
Pooran Prasad Rajanna | calendar 08 September 2020 | 3348

Simple function to check if there is internet connection.. usually from desktop app

Sample Code

public bool ConnectionAvailable(string yourfavoritewebsite)
        {
            try
            {
                HttpWebRequest reqFP = (HttpWebRequest)HttpWebRequest.Create(yourfavoritewebsite);

                HttpWebResponse rspFP = (HttpWebResponse)reqFP.GetResponse();
                if (HttpStatusCode.OK == rspFP.StatusCode)
                {
                    rspFP.Close();
                    return true;
                }
                else
                {
                    rspFP.Close();
                    return false;
                }
            }
            catch (WebException)
            {
                MessageBox.Show("No Internet connection!");
                return false;
            }
        }