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)?
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.
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.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With