wpf function to check if internet connection is available Edit

Pooran Prasad Rajanna | 08 September 2020 | 814

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;
            }
        }