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

I can not find any information on my question. Please excuse me if my search efforts were not good enough to find an answer. I just want to not spin the wheels.

Thanks!

Followup: If it is not overwritten, how can I get it (if possible)?

+7
source share
3 answers

30 second test confirms overwriting

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 overwritten if downlaoded again

+22
source

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

I know for sure that this solution in codeproject contains a class that loads a file using WebRequest and WebResponse , which allows a lot more control. See A class containing the name webdata . Code you can pay attention to:

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

FileMode Enumeration contains a number of members that dictate the FileMode.CreateNew file save behavior, will throw an IOException if the file already exists. Like where FileMode.Create will overwrite files if possible.

If you insist on using WebClient.DownloadFileAsync , then, as other guys have already noted, you can simply tell the user that the existing file will be overwritten with OpenFileDialog , but some downloads can take a long time and there is nothing to say that the user did not create another file at boot time.

+2
source

If the file exists, yes.

If you rename it, or if you connected it to OpenFileDialogue() , this is your discretion.

0
source

All Articles