How to Check Image is Exist or not on Server

To check from Image path that it is exist or not on server –

 
if (UrlExists(ImageURL))
{
Image1.ImageUrl = ImageURL;
}
else
{
Image1.ImageUrl = NoImageURL;
}

private static bool UrlExists(string url)
{
try
{
new System.Net.WebClient().DownloadData(url);
return true;
}
catch (System.Net.WebException e)
{
//if (((System.Net.HttpWebResponse)e.Response).StatusCode == System.Net.HttpStatusCode.NotFound)
return false;
//else
throw;
}
}

Advertisement