How can I check a File exits in a web location in ASP.Net(in a different web application, but same web server), currently I doing like this. Is there any better way of doing this?
using (WebClient client = new WebClient())
{
try
{
Stream stream = client.OpenRead("http://localhost/images/myimage.jpg");
if (stream != null)
{
//exists
}
}
catch
{
//Not exists
}
}
If a file is accessible via HTTP, you can issue a HTTP HEAD requrest for that particular URL using
HttpWebRequest. IfHttpWebResponse.StatusCodewill be 200, than file is there.EDIT: See this on why
GetResponsethrows stupid exceptions when it actually should not do that.