下载完成后WebClient关闭应用程序

时间:2011-02-22 20:01:23

标签: c# .net wpf webclient

我正在使用WebClient下载一个巨大的文件(700 + MB)。下载完成后,应用程序就会自行关闭。我试过调试,但无法捕捉任何东西。

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var wc = new WebClient();
        wc.DownloadDataAsync(new Uri(@"http://192.168.1.100/FileServer/crypto.bin"));

    }
}

这是一个已知的错误吗?

2 个答案:

答案 0 :(得分:2)

您必须添加一个事件,例如:

Private void btnDownload_Click(object sender, EventArgs e)
{
  WebClient webClient = new WebClient();
  webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
  webClient.DownloadFileAsync(new Uri("http://mysite.com/myfile.txt"), @"c:\myfile.txt");
}

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

答案 1 :(得分:1)

我认为问题是您必须在初始化程序之外声明de wc变量。

    WebClient wc;

    public MainWindow()
    {
        InitializeComponent();

        wc = new WebClient();
        wc.DownloadDataAsync(new Uri(@"http://192.168.1.100/FileServer/crypto.bin"));

    }