Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does WebClient.DownloadFileAsync overwrite the file if it already exists on disk?

I can't find any information on my question. Please excuse me if my search efforts have not been good enough to find the answer. I just want to avoid spinning my wheels.

Thanks!

Follow up: If it doesn't overwrite, how can I get it to (if possible)?

like image 989
Kashif Avatar asked Jan 15 '13 21:01

Kashif


People also ask

Does system net web client downloadfile overwrite the file?

That is the reason I am using System.Net.Webclient.Downloadfile () method. I think it will overwrite only when there is a change in the file present at server.

Why do I receive an error when using the download method?

When using this method in an ASP.NET page, you will receive an error if the account that the page executes under does not have permission to access the local file. Downloads, to a local file, the resource with the specified URI. This method does not block the calling thread. The URI of the resource to download.

What to do if the file does not exist on server?

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. If the file does not exist, this code will throw an exception. You can catch and ignore that exception. Or alternatively, you can do it in two steps.


2 Answers

A 30 second test confirms that it does overwrite

Test:

using (WebClient client = new WebClient())
{
    client.DownloadFileAsync(new Uri("http://download.microsoft.com/download/8/4/A/84A35BF1-DAFE-4AE8-82AF-AD2AE20B6B14/directx_Jun2010_redist.exe"), @"C:\Test.exe");
}

Test.exe is overwitten if downlaoded again

like image 106
sa_ddam213 Avatar answered Sep 18 '22 14:09

sa_ddam213


The WebClient class is obviously designed to suppress a lot of detail and control. You can write your own method to asynchronously download a file very easily and control how the downloaded data is written to disc.

I know for sure that this solution at codeproject contains a class which downloads a file using WebRequest and WebResponse which allows for much more control. See the class contained named webdata. The code you can need to pay attention too:

FileStream newFile = new FileStream(targetFolder + file, FileMode.Create);
newFile.Write(downloadedData, 0, downloadedData.Length);
newFile.Close();

The FileMode Enumeration contains a series of members that dictate the behaviour of saving a file FileMode.CreateNew will throw an IOException if a file already exists. As where FileMode.Create will overwrite files if possible.

If you insist on using WebClient.DownloadFileAsync then, as the other fellas have already mentioned: you can just inform the user that an existing file will be overwritten by means of an OpenFileDialog but some downloads can be time consuming and there's nothing to say that the user has not created another file during the download.

like image 44
Caster Troy Avatar answered Sep 20 '22 14:09

Caster Troy