2

I would like to know how to check if the file exists before downloading.

Current code:

string url = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "/text.txt";
string path = "asdf.wix.com/text.txt";

using (var client = new WebClient())
{
    client.DownloadFile(url, path);
}

The code works but if the file is missing on the site it creates a empty text.txt that causes issues.

Any ideas? Thank you!

Blackwood
  • 4,504
  • 16
  • 32
  • 41
maximumRider
  • 21
  • 1
  • 3

2 Answers2

3

If the url var points to a location on the PC, then you could check for existence with System.IO.File.Exists:

if(!System.IO.File.Exists(url))
{
    //code that handles the file dne case.. maybe log and return?
}

If it's pointing towards a remote location, then I'm not really sure how to check for its existence beforehand.

You could, however, handle the 404 case returned by WebClient and delete the erroneous text.txt file

using (var client = new WebClient())
{
    try
    {
        client.DownloadFile(url, path);
    }
    catch (WebException e)
    {
        var statusCode = ((HttpWebResponse) e.Response).StatusCode;

        if (statusCode == HttpStatusCode.NotFound && System.IO.File.Exists(path))
        {
            System.IO.File.Delete(path);
            //maybe log the occurence as well
        }
    }
}
Jared Lovin
  • 543
  • 9
  • 24
  • so something like write to random directory then verify bytes are > 0 then File.move(randomdirectory, path) or do you have a better way – maximumRider Sep 12 '17 at 00:06
  • I have a feeling that that would be error prone. I've updated my answer as to what I would do in your case. – Jared Lovin Sep 12 '17 at 00:15
0

Consider using HttpWebRequest instead of WebClient if you need specific behavior. WebClient does a lot of "automagic".

var request = (HttpWebRequest)WebRequest.Create(url);
request.Method = "GET";
using (var response = request.GetResponse()) {
    using (var responseStream = response.GetResponseStream()) {   
        using (var fileToDownload = new System.IO.FileStream(path,  System.IO.FileMode.Create,  System.IO.FileAccess.ReadWrite)) {
            responseStream.CopyTo(fileToDownload);
        }
    }
}

Using this approach you have control over when the downloaded file gets created-- that is after you have started downloading. If the file does not exist on the server, it will error before the file is created. You can add additional error checking before creating the FileStream such as checking expected content type, etc.

Tim
  • 5,940
  • 1
  • 12
  • 18
  • thanks works fine took me a min to figure out how to close the file after writing but i got it now – maximumRider Sep 13 '17 at 00:49
  • If the file does not exist, this code will throw an exception. You can catch and ignore that exception. – feroze Sep 13 '17 at 13:45
  • Or alternatively, you can do it in two steps. In the first step, use the same code as above, but without the call to GetResponseStream(), and use HEAD as the request method. This will return 200 if the file exists. It will also return a content-length header. You can check for 200 status, and conent-length > 200 to make sure the file exists before downloading. – feroze Sep 13 '17 at 13:46