11

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)?

4

3 回答 3

41

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

于 2013-01-15T21:28:41.167 回答
3

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.

于 2013-01-15T21:49:39.680 回答
0

If the file exists, yes.

If you are renaming it, or if you have it hooked into an OpenFileDialogue(), that's your discretion.

于 2013-01-15T21:34:09.217 回答