Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I was wondering if there is a easy way to detect if a file still exists on the net. I know that the error code for it is 404 but how would you detect it.

What I'd like is to have a method that returns false or true if I give the method a direct url link (www.somesite/filename.zip).
Posted
Updated 8-Mar-10 6:55am
v2

1 solution

C#
private static bool FileExists(string url)
{
    bool b = true;


    try
    {
        WebClient wc = new WebClient();
        wc.OpenRead(url);
    }
    catch (WebException ex)
    {
        //(ex.Response as HttpWebResponse).StatusCode == HttpStatusCode.NotFound)
        b = false;
    }
    return b;
}


Note that this will work but there might be several reasons (server down, access denied) why a connection may fail.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900