Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Efficient way to check if web resource/file exists?

Efficient way to check if web resource/file exists?

Scheduled Pinned Locked Moved C#
comsysadminhelptutorialquestion
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dominic Pettifer
    wrote on last edited by
    #1

    Whats the best most efficient way of checking whether a resource (image, page, document etc.) exists at a particular URL web address location. Eg. I have a url http://www.example.com/myImage.jpg and I want to check whether myImage.jpg actually exists. At the moment I'm using the WebClient class like so... WebClient client = new WebClient(); try {   if(client.Download("http://www.example.com/myImage.jpg").Length > 0)   {     // File exists   }   else   {     // File doesn't exist   } } catch {   // File doesn't exist } ...problem with this method though is that it downloads the entire file to check it's existence. I'm not interested in the file, just whether the server will return a 404 or not. Cheers!

    Dominic Pettifer Blog: www.dominicpettifer.co.uk

    I 2 Replies Last reply
    0
    • D Dominic Pettifer

      Whats the best most efficient way of checking whether a resource (image, page, document etc.) exists at a particular URL web address location. Eg. I have a url http://www.example.com/myImage.jpg and I want to check whether myImage.jpg actually exists. At the moment I'm using the WebClient class like so... WebClient client = new WebClient(); try {   if(client.Download("http://www.example.com/myImage.jpg").Length > 0)   {     // File exists   }   else   {     // File doesn't exist   } } catch {   // File doesn't exist } ...problem with this method though is that it downloads the entire file to check it's existence. I'm not interested in the file, just whether the server will return a 404 or not. Cheers!

      Dominic Pettifer Blog: www.dominicpettifer.co.uk

      I Offline
      I Offline
      I explore code
      wrote on last edited by
      #2

      Include System.IO and System.Text namespace to your project Then you would be able to access File class which has a method defined for checking the existence of files as shown below if (File.Exists(@"//localhost/command.gif") == false) { Response.Redirect("FileNotFound.htm"); } else { Response.Write("File Exists"); } I tested this code on my localhost, it works fine. I think this code would access the client machines path for the file and not the server's, just verify and let me know. -- modified at 9:37 Monday 25th June, 2007

      L 1 Reply Last reply
      0
      • I I explore code

        Include System.IO and System.Text namespace to your project Then you would be able to access File class which has a method defined for checking the existence of files as shown below if (File.Exists(@"//localhost/command.gif") == false) { Response.Redirect("FileNotFound.htm"); } else { Response.Write("File Exists"); } I tested this code on my localhost, it works fine. I think this code would access the client machines path for the file and not the server's, just verify and let me know. -- modified at 9:37 Monday 25th June, 2007

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        gladiatron wrote:

        I tested this code on my localhost, it works fine. I think this code would access the client machines path for the file and not the server's, just verify and let me know.

        Just tested it with IronPython:

        >>> ex = File.Exists("http://www.codeproject.com/script/images/news\_answer.gif")

        ex
        False

        regards

        1 Reply Last reply
        0
        • D Dominic Pettifer

          Whats the best most efficient way of checking whether a resource (image, page, document etc.) exists at a particular URL web address location. Eg. I have a url http://www.example.com/myImage.jpg and I want to check whether myImage.jpg actually exists. At the moment I'm using the WebClient class like so... WebClient client = new WebClient(); try {   if(client.Download("http://www.example.com/myImage.jpg").Length > 0)   {     // File exists   }   else   {     // File doesn't exist   } } catch {   // File doesn't exist } ...problem with this method though is that it downloads the entire file to check it's existence. I'm not interested in the file, just whether the server will return a 404 or not. Cheers!

          Dominic Pettifer Blog: www.dominicpettifer.co.uk

          I Offline
          I Offline
          I explore code
          wrote on last edited by
          #4

          Your question is very vague..why do you need to write the code to check for file existence on some other web server ? Its a built in thing, if u request for a non-existent page, the logic on that server will automatically handle the request and show you an error page. However, if u wanna do this for your own server you can, using the below given code. if (File.Exists(Server.MapPath("web.gif"))) { Response.Write("File Exists"); } else { Response.Redirect("FileNotFound.htm"); } This code will map the virtual path of your server so if some client browser requests for a file that does not exist on your web server, he will get an error page other wise the page will load, now it depends on you how you customize the application for your clients.

          D 1 Reply Last reply
          0
          • I I explore code

            Your question is very vague..why do you need to write the code to check for file existence on some other web server ? Its a built in thing, if u request for a non-existent page, the logic on that server will automatically handle the request and show you an error page. However, if u wanna do this for your own server you can, using the below given code. if (File.Exists(Server.MapPath("web.gif"))) { Response.Write("File Exists"); } else { Response.Redirect("FileNotFound.htm"); } This code will map the virtual path of your server so if some client browser requests for a file that does not exist on your web server, he will get an error page other wise the page will load, now it depends on you how you customize the application for your clients.

            D Offline
            D Offline
            Dominic Pettifer
            wrote on last edited by
            #5

            Long story but we have a list of external URLs which point to images on another web server, which we need to display on our site via an img tag or asp:Image control. Thing is some of these URLS are incorrect and point to images that don't exist, and rather than display an empty image box with a red X, they want a replacement image instead. Basically, they want us to guard against their own bad data. I know it's additional overhead but it's what they want, I think I can optimise it by caching the state of the external image for a time. But File.Exists("http://www.example.com/image123.jpg"); should do the trick. If you know of a more efficient way let me know. Cheers! :)

            Dominic Pettifer Blog: www.dominicpettifer.co.uk

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups