您好我试图制作一个可以下载文件问题的程序,当它下载一个超过1GB的文件时崩溃和中断有一种方法可以使它可以下载更大的文件这是我使用
的代码private void button1_Click(object sender, EventArgs e)
{
WebClient web = new WebClient();
string listbox = listBox1.SelectedItem.ToString();
web.DownloadFileAsync(new Uri(http://example.com/file.avi), location" + "file.avi");
web.DownloadProgressChanged += new DownloadProgressChangedEventHandler(web_DownloadProgressChanged);
web.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
}
private void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
// Place for a message when the downloading has compleated
}
void web_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
int bytesin = int.Parse(e.BytesReceived.ToString());
int totalbytes = int.Parse(e.TotalBytesToReceive.ToString());
int kb1 = bytesin / 1024;
int kb2 = totalbytes / 1024;
toolStripStatusLabel1.Text = kb1.ToString() + "KB out of " + kb2.ToString() + "KB (" + e.ProgressPercentage.ToString() + "%)";
progressBar1.Value = e.ProgressPercentage;
}
答案 0 :(得分:1)
此
CXX_STD
导致您的问题。您正在将long转换为int并在BytesReceived或TotalBytesToReceive转到int32.MaxValue之后获得OverflowException。
将方法更改为以下内容:
void web_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
int bytesin = int.Parse(e.BytesReceived.ToString());
int totalbytes = int.Parse(e.TotalBytesToReceive.ToString());
int kb1 = bytesin / 1024;
int kb2 = totalbytes / 1024;
toolStripStatusLabel1.Text = kb1.ToString() + "KB out of " + kb2.ToString() + "KB (" + e.ProgressPercentage.ToString() + "%)";
progressBar1.Value = e.ProgressPercentage;
}