【问题标题】:C# Downloading a file does not workC# 下载文件不起作用
【发布时间】:2013-06-28 13:36:45
【问题描述】:

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

谢谢。

【问题讨论】:

    标签: c# webclient downloading-website-files


    【解决方案1】:

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

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

    【讨论】:

    • 如果这是你的答案,那你为什么接受另一个答案?
    【解决方案2】:

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

     webClient.DownloadFile(_fileToDownload, _filePath);
    

    【讨论】:

    • 它不想工作。我在已经运行的后台工作人员中使用代码,当我将其更改为非异步下载方法时,它说我的后台工作人员的工作已完成(这是错误的,因为它应该工作一段时间)。 .. 这只发生在 DownloadFile 方法而不是 ASync 方法上,但 ASync 方法不会下载任何东西
    【解决方案3】:

    所以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);
    }
    

    【讨论】:

    • 理论上如何启动任务?
    • webclient.DownloadFileAsync(new Uri(_fileToDownload ), @_filePath).Start() 应该可以工作,或者如果这是在 async 方法中,您总是可以只使用 await webclient.DownloadFileAsync(new Uri(_fileToDownload ), @_filePath);
    【解决方案4】:

    使用同步下载方式。

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

    【讨论】:

    • 它不想工作。我在已经运行的后台工作程序中使用代码,当我将其更改为非异步下载方法时,它说我的后台工作程序的工作已完成(这是错误的,因为它应该工作一段时间)。 .. 这只发生在 DownloadFile 方法而不是 ASync 方法上,但 ASync 方法不会下载任何东西
    【解决方案5】:

    试试这个代码-

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

    【讨论】:

    • 使用 System.Net.WebClient.Download
    猜你喜欢
    • 2013-04-12
    • 2011-03-13
    • 1970-01-01
    • 1970-01-01
    • 2012-11-26
    • 2020-12-28
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    相关资源
    最近更新 更多