ciao a tutti oggi ho fatto questo downloader però mi da errore...alla linea rossa e mi sono scervellato per capire cosa c'e che non va ma non ci sono riuscito...aiutatemi!
Codice:
namespace DownloadManager2
{
public partial class Form1 : Form
{
private Thread thrDownload;
private Stream strResponse;
private Stream strLocal;
private HttpWebRequest webRequest;
private HttpWebResponse webResponse;
private static int PercentProgress;
private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
bool goPause = false;
public Form1()
{
InitializeComponent();
}
private void btnDownload_Click(object sender, EventArgs e)
{
if (thrDownload != null && thrDownload.ThreadState == ThreadState.Running)
{
MessageBox.Show("Impossibile avviare download. Aspettare che sia completato il primo", "Download in progress", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
lblProgress.Text = "Avviando Download";
thrDownload = new Thread(new ParameterizedThreadStart(Download));
thrDownload.Start(0);
btnPauseResume.Enabled = true;
btnPauseResume.Text = "Pausa";
}
}
private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
PercentProgress = Convert.ToInt32((BytesRead * 100) / TotalBytes);
prgDownload.Value = PercentProgress;
lblProgress.Text = "Scaricato " + BytesRead + " di " + TotalBytes + " (" + PercentProgress + "%)";
}
private void Download(object startPoint)
{
try
{
int startPointInt = Convert.ToInt32(startPoint);
webRequest = (HttpWebRequest)WebRequest.Create(txtUrl.Text);
webRequest.AddRange(startPointInt);
webRequest.Credentials = CredentialCache.DefaultCredentials;
[COLOR="#ff0000"]webResponse = (HttpWebResponse)webRequest.GetResponse();[/COLOR]
Int64 fileSize = webResponse.ContentLength;
strResponse = webResponse.GetResponseStream();
if (startPointInt == 0)
{
strLocal = new FileStream(txtPath.Text, FileMode.Create, FileAccess.Write, FileShare.None);
}
else
{
strLocal = new FileStream(txtPath.Text, FileMode.Append, FileAccess.Write, FileShare.None);
}
int bytesSize = 0;
byte[] downBuffer = new byte[2048];
while ((bytesSize = strResponse.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
strLocal.Write(downBuffer, 0, bytesSize);
this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { strLocal.Length, fileSize + startPointInt });
if (goPause == true)
{
break;
}
}
}
finally
{
strResponse.Close();
strLocal.Close();
}
}
private void btnStop_Click(object sender, EventArgs e)
{
thrDownload.Abort();
webResponse.Close();
strResponse.Close();
strLocal.Close();
prgDownload.Value = 0;
lblProgress.Text = "Download stoppato";
btnPauseResume.Enabled = false;
}
private void btnPauseResume_Click(object sender, EventArgs e)
{
if (thrDownload != null)
{
if (