Friday, September 24, 2010

How can one check to see if a remote file exists using C#

private void MyFunction()
{
string strFilePath = "http://localhost/images/myimage.jpg";
Uri filePath = new Uri(strFilePath );

if()
{
// File Exists..

}
else
{
// File Does not Exists..

}


}


bool remoteFileExists(string addressOfFile)
{
try
{
HttpWebRequest request = WebRequest.Create(addressOfFile) as HttpWebRequest;
request.Method = "HEAD";
request.CachePolicy = new RequestCachePolicy(RequestCacheLevel.NoCacheNoStore);
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
return (response.StatusCode == HttpStatusCode.OK);
}
catch(WebException wex)
{
return false;
}
}

No comments:

Post a Comment