C#下载文件不起作用

时间:2013-06-28 13:36:46

标签: c# webclient-download downloading-website-files

使用下面的代码,但它根本没有将任何文件下载到名为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);
}

感谢。

5 个答案:

答案 0 :(得分:3)

确定。 emmmmmmmmm找到了我的答案:确实我可以使用filedownload方法但是输入正确的保存路径对我来说很有用...这是正确的路径,我忘了在我的文件夹名称之前加上\ .... / p>

string _filePath = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location) + "\\MyImages\\download24.png'";

答案 1 :(得分:1)

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

 webClient.DownloadFile(_fileToDownload, _filePath);

答案 2 :(得分: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);
}

答案 3 :(得分:0)

使用同步下载方法。

  webClient.DownloadFile(new Uri(_fileToDownload ), @_filePath);

答案 4 :(得分: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);