Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can I check if a file exists at a URL?

Tags:

c#

file-io

I know I can locally, on my filesystem, check if a file exists:

if(File.Exists(path)) 

Can I check at a particular remote URL?

like image 826
mrblah Avatar asked Dec 30 '09 12:12

mrblah


People also ask

How do I find the URL of a file?

To obtain a file or folder's URL, to the right of the file or folder, from the Actions drop-down menu, select Edit Details. This displays the Edit Details page for the item.

How can I tell if a text file exists?

To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True .

Which function is used to check file existence?

The file_exists() function checks whether a file or directory exists.

How check if file exists folder?

To check for specific files use File. Exists(path) , which will return a boolean indicating wheter the file at path exists.


2 Answers

If you're attempting to verify the existence of a web resource, I would recommend using the HttpWebRequest class. This will allow you to send a HEAD request to the URL in question. Only the response headers will be returned, even if the resource exists.

var url = "http://www.domain.com/image.png"; HttpWebResponse response = null; var request = (HttpWebRequest)WebRequest.Create(url); request.Method = "HEAD";   try {     response = (HttpWebResponse)request.GetResponse(); } catch (WebException ex) {     /* A WebException will be thrown if the status of the response is not `200 OK` */ } finally {     // Don't forget to close your response.     if (response != null)     {         response.Close();     } } 

Of course, if you want to download the resource if it exists it would most likely be more efficient to send a GET request instead (by not setting the Method property to "HEAD", or by using the WebClient class).

like image 131
Justin Rusbatch Avatar answered Sep 22 '22 09:09

Justin Rusbatch


If you want to just copy & paste Justin's code and get a method to use, here's how I've implemented it:

using System.Net;  public class MyClass {     static public bool URLExists (string url) {         bool result = false;          WebRequest webRequest = WebRequest.Create(url);         webRequest.Timeout = 1200; // miliseconds         webRequest.Method = "HEAD";          HttpWebResponse response = null;          try {             response = (HttpWebResponse)webRequest.GetResponse();             result = true;         } catch (WebException webException) {             Debug.Log(url +" doesn't exist: "+ webException.Message);         } finally {             if (response != null) {                 response.Close();             }         }          return result;     } } 

I'll keep his observation:

If you want to download the resource, and it exists, it would be more efficient to send a GET request instead by not setting the Method property to "HEAD" or by using the WebClient class.

like image 30
cregox Avatar answered Sep 20 '22 09:09

cregox