1

Using code below but it is not downloading any file at all to the specified subfolder named myImages to the application... How can I fix this? The link is purely an example here, usually the link will be a variable and that has no issue populating itself. This is all being done in a BackgroundWorker that works 100% fine otherwise.

//File to download
string _fileToDownload = "http://www.codeproject.com/images/download24.png"
//Path to download files
string _filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "MyImages\\download24.png'";
//Download the image
using (var webClient = new WebClient())
{
    webClient.DownloadFileAsync(new Uri(_fileToDownload ), @_filePath);
}

Thanks.

touyets
  • 1,315
  • 6
  • 19
  • 34

5 Answers5

3

Ok. emmmmmmmmm found my answer: indeed I can use the filedownload method but it would have been useful for me to enter the correct save path... this was the correct path, I forgot to put a \ before my folder name....

string _filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\MyImages\\download24.png'";
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
touyets
  • 1,315
  • 6
  • 19
  • 34
1

In your case there is no need to use asynchronous file download. This should work:

 webClient.DownloadFile(_fileToDownload, _filePath);
Ahmed KRAIEM
  • 10,267
  • 4
  • 30
  • 33
  • It doesn't want to work. I am using the code in a background worker that is already running and the minute I change it to a non async method for download, it says mthat my backgroundworker's work is completed (which is wrong since it should be working for a while longer)... this only happens with the DownloadFile method and not the ASync methiod but the ASync method doesn't download anything – touyets Jun 28 '13 at 14:24
0

So the WebClient.DownloadFileAsync method only creates a Task to download the file you need to actually start the task to download the file (by either calling Task.Start or Task.RunSyncronously) or you can call the syncronous API

//File to download
string _fileToDownload = "http://www.codeproject.com/images/download24.png"
//Path to download files
string _filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "MyImages\\download24.png'";
//Download the image
using (var webClient = new WebClient())
{
    //Set the banner variable
     webClient.DownloadFile(new Uri(_fileToDownload ), @_filePath);
}
Mgetz
  • 5,108
  • 2
  • 33
  • 51
  • How could I launch the task in theory? – touyets Jun 28 '13 at 14:25
  • `webclient.DownloadFileAsync(new Uri(_fileToDownload ), @_filePath).Start()` should work, or if this is in an `async` method you can always just `await webclient.DownloadFileAsync(new Uri(_fileToDownload ), @_filePath);` – Mgetz Jun 28 '13 at 14:26
0

Use the synchronous download method.

  webClient.DownloadFile(new Uri(_fileToDownload ), @_filePath);
apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
  • It doesn't want to work. I am using the code in a background worker that is already running and the minute I change it to a non async method for download, it says mthat my backgroundworker's work is completed (which is wrong since it should be working for a while longer)... this only happens with the DownloadFile method and not the ASync methiod but the ASync method doesn't download anything – touyets Jun 28 '13 at 14:23
0

Try this Code-

string remoteUri = "http://www.codeproject.com/images/";
string fileName = "download24.png"; 
StringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName,   myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName);     
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);
Dinesh
  • 507
  • 4
  • 14
  • 23