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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s