【问题标题】:WebClient isn't downloading file, but returns CompletedWebClient 不下载文件,但返回 Completed
【发布时间】:2012-08-24 08:17:53
【问题描述】:

我的代码如下。我正在尝试下载一个文件,由于这段代码,我的应用程序给了我“找不到文件名 - 正在下载”,然后是“完成”,但是当我去查看时,实际上并没有下载任何文件。

    private void Form1_Load(object sender, EventArgs e)
    {
        download(@"mp3spi.jar", Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"\.minecraft\bin\lob\");
    }

    public void download(String filename, String path)
    {
        filenameLabel.Text = filename;
        MessageBox.Show(filename + " Not found - downloading.");
        WebClient webClient = new WebClient();
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
        webClient.DownloadFileAsync(new Uri("http://mysite.com/client/" + filename), path);
    }

    public void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        progressBar1.Value = e.ProgressPercentage;
    }

    private void Completed(object sender, AsyncCompletedEventArgs e)
    {
        MessageBox.Show("Finished.");
    }

谁能指出我出了什么问题?我想也许它需要以管理员身份运行,但这只是做了同样的事情。

【问题讨论】:

    标签: c# webclient


    【解决方案1】:

    您的代码尝试保存到文件夹路径 ("...\lob\"),它不是文件名 (...\lob\my_file.ext")。

    WebClient.DownloadFileAsync(Uri Uri address, string fileName)

    【讨论】:

    • 啊,谢谢,总是那些小事让我感动 :P 我会在 5 分钟内接受 :)
    猜你喜欢
    • 2013-06-03
    • 2015-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多