1

使用下面的代码,但它根本没有将任何文件下载到名为 myImages 的指定子文件夹到应用程序......我该如何解决这个问题?该链接在这里纯粹是一个示例,通常该链接将是一个变量,并且填充自身没有问题。这一切都在 BackgroundWorker 中完成,否则 100% 可以正常工作。

//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);
}

谢谢。

4

5 回答 5

3

好的。emmmmmmmmm 找到了我的答案:确实我可以使用 filedownload 方法,但输入正确的保存路径对我很有用...这是正确的路径,我忘了在我的文件夹名称前放一个 \...

string _filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\MyImages\\download24.png'";
于 2013-06-28T14:35:49.857 回答
1

在您的情况下,无需使用异步文件下载。这应该有效:

 webClient.DownloadFile(_fileToDownload, _filePath);
于 2013-06-28T13:41:46.733 回答
0

试试这个代码 -

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);
于 2013-06-28T13:47:53.547 回答
0

因此,该WebClient.DownloadFileAsync方法只创建一个Task下载文件,您需要实际启动下载文件的任务(通过调用Task.StartTask.RunSyncronously),或者您可以调用同步 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);
}
于 2013-06-28T13:40:28.310 回答
0

使用同步下载方式。

  webClient.DownloadFile(new Uri(_fileToDownload ), @_filePath);
于 2013-06-28T13:47:14.713 回答