下载Async无法完成下载c#

时间:2016-06-21 17:40:29

标签: c# file asynchronous download webclient

我正在尝试制作一个更新程序,它将从weblient以异步模式从URL下载。

但它削减了我的下载并且没有下载完整的文件。

以下是代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.Diagnostics;

namespace Updater
{
  public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        WebClient Client = new WebClient();

        Client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
        Client.DownloadFileAsync(new Uri("http://localhost/launcher/"), @"C:\launcher.exe");
    }

    private void Completed(object sender, AsyncCompletedEventArgs e)
    {

        if (e.Error != null)
        {
            string error = e.Error.ToString();
            // or
            // error = "Your error message here";

            MessageBox.Show(error);
            return;
        }

        if (e.Cancelled == true)
        {
        }
        else
        {
            Close();
        }

    }
}

}

我是c#的新手,仍然无法确定我的错误在哪里。 :/ TY

编辑:

我错过了说下载文件launcher.exe:

 Client.DownloadFileAsync(new Uri("http://localhost/launcher/"), @"C:\launcher.exe");

到这个

 Client.DownloadFileAsync(new Uri("http://localhost/launcher/launcher.exe"), @"C:\launcher.exe");

感谢АдизбекЭргашев的链接及其帮助我:https://stackoverflow.com/a/26232827/6495475

0 个答案:

没有答案