Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if a file exists on a server using c# and the WebClient class

Tags:

In my application I use the WebClient class to download files from a Webserver by simply calling the DownloadFile method. Now I need to check whether a certain file exists prior to downloading it (or in case I just want to make sure that it exists). I've got two questions with that:

  1. What is the best way to check whether a file exists on a server without transfering to much data across the wire? (It's quite a huge number of files I need to check)
  2. Is there a way to get the size of a given remote file without downloading it?

Thanks in advance!

like image 714
Mats Avatar asked May 06 '09 16:05

Mats


People also ask

How do I check if a file exists in C?

access() Function to Check if a File Exists in C Another way to check if the file exists is to use the access() function. The unistd. h header file has a function access to check if the file exists or not. We can use R_OK for reading permission, W_OK for write permission and X_OK to execute permission.

How do you check if a file is in a directory in C?

The isDir() function is used to check a given file is a directory or not.

How do you check if a file already 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 . Otherwise, it returns False .

How do you check if a file is present in a directory?

isdir() Method to check if file exists. os. path. isdir() method in Python is used to check whether the specified path is an existing directory or not.


2 Answers

WebClient is fairly limited; if you switch to using WebRequest, then you gain the ability to send an HTTP HEAD request. When you issue the request, you should either get an error (if the file is missing), or a WebResponse with a valid ContentLength property.

Edit: Example code:

WebRequest request = WebRequest.Create(new Uri("http://www.example.com/")); request.Method = "HEAD";  using(WebResponse response = request.GetResponse()) {    Console.WriteLine("{0} {1}", response.ContentLength, response.ContentType); } 
like image 135
Tim Robinson Avatar answered Sep 22 '22 14:09

Tim Robinson


When you request file using the WebClient Class, the 404 Error (File Not Found) will lead to an exception. Best way is to handle that exception and use a flag which can be set to see if the file exists or not.

The example code goes as follows:

System.Net.HttpWebRequest request = null; System.Net.HttpWebResponse response = null; request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create("www.example.com/somepath"); request.Timeout = 30000; try {     response = (System.Net.HttpWebResponse)request.GetResponse();     flag = 1; } catch  {     flag = -1; }  if (flag==1) {     Console.WriteLine("File Found!!!"); } else {     Console.WriteLine("File Not Found!!!"); } 

You can put your code in respective if blocks. Hope it helps!

like image 21
Code Avatar answered Sep 23 '22 14:09

Code