her er et eksempel:
try {
string imgPath = "
http://www.google.com/images/logo.gif"; HttpWebRequest wRequest = ((HttpWebRequest)(HttpWebRequest.Create(imgPath)));
WebResponse wResponse = ((WebResponse)(wRequest.GetResponse()));
FileStream sWriter = new FileStream("new.gif", FileMode.OpenOrCreate);
int ContentLength = wResponse.ContentLength;
string sizeInKB = (ContentLength / 1024).ToString();
this.Text = "The Size of the Image is: " + sizeInKB.Substring(0, sizeInKB.IndexOf(".") + 3) + "KB";
byte[] buffer = new byte[ContentLength];
int length = ContentLength;
int position = 0;
int complete = 1;
int returned = 0;
ProgressBar1.Value = 0;
ProgressBar1.Maximum = ContentLength;
while (!(complete == 0)) {
position = wResponse.GetResponseStream().Read(buffer, returned, length);
sWriter.Write(buffer, returned, position);
complete = position;
returned += position;
length -= position;
ProgressBar1.Step = returned;
ProgressBar1.PerformStep();
}
this.Text = "Completed download";
sWriter.Close();
wRequest = null;
} catch (Exception ex) {
Console.WriteLine(ex.Message);
} catch (WebException webex) {
Console.WriteLine(webex.Message);
}