1

I am using this to download a file and get % info and completed info. I want to know how to get the size of the file being downloaded and the URL remote address and the local address where the file is being saved to.

private void Form1_Load_1(object sender, EventArgs e)
        {
     label21.Text = "Download in progress...";
            WebClient webClient = new WebClient();
     webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
     webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
     webClient.DownloadFileAsync(new Uri("http://www.somesite.com/Update/Updates.zip.010"), @"Updates.zip.010");
 }

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage; //Progress Bar Handler
            label1.Visible = true;
            label1.Text = progressBar1.Value.ToString() + " %"; //Adds percent to a label
        }

private void Completed(object sender, AsyncCompletedEventArgs e)
        {
            label11.Visible = true;
            label11.Text = "Done";
        }
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
Jay
  • 23
  • 1
  • 1
  • 5
  • Can you add the right language tag? – Pekka Sep 18 '10 at 13:12
  • Is this an ASP.NET application or WinForms? – Darin Dimitrov Sep 18 '10 at 13:24
  • Hello this is C# windows form. – Jay Sep 18 '10 at 13:44
  • 1
    Well, then let's tag it that way. – tvanfosson Sep 18 '10 at 13:45
  • YEP it's tagged now thanks for pointing that out. Brain sizzle. – Jay Sep 18 '10 at 16:02
  • OK searching around I found this post and its the closest to what I think I need to do but I can not make it work. I am trying to get the file size to show in the label. Here is the code I tried. – Jay Sep 18 '10 at 17:06
  • HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://www.somesite.com/Update/Updates.zip.001"); req.Method = "HEAD"; HttpWebResponse resp = (HttpWebResponse)(req.GetResponse()); int len = resp.ContentLength; { label1.Text = "ContentLength"; } – Jay Sep 18 '10 at 17:32
  • I got that info form here and am trying to get it to show the file size in a lable. http://stackoverflow.com/questions/122853/c-get-http-file-size – Jay Sep 18 '10 at 17:39
  • I figured out a method but I dont know if this is very good or not. Here is the code it will get megabytes, kilobytes, and bytes and multiply it by 10 to get the total size of10 HTTP URL files that are equal in size to be downloaded. Problem is some of the files dont show in my progress bar any more when they are downloading but some do show. – Jay Sep 21 '10 at 04:36
  • System.Net.WebRequest req = System.Net.HttpWebRequest.Create("http://www.somesite.com/Update/Updates.zip.001");req.Method = "HEAD";System.Net.WebResponse resp = req.GetResponse();int ContentLength;if (int.TryParse(resp.Headers.Get("Content-Length"), out ContentLength)){// Do math equation to get bytes to equal megabytes etc. The number * 10 means there is times 10 files textBox1.Text += ContentLength / 1024.0 * 10 / 1024.0 + " MB";textBox2.Text += ContentLength / 1024.0 * 10 + " KB";textBox3.Text += ContentLength * 10 + " Bytes";} – Jay Sep 21 '10 at 04:39
  • Possible duplicate of [Get http:/.../File Size](http://stackoverflow.com/questions/122853/get-http-file-size) –  Mar 18 '17 at 19:05

1 Answers1

7

I just rewrote what Jay wrote as comments to his own question, so that it'll be easier to read:

HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://cdn.sstatic.net/stackoverflow/img/favicon.ico");
req.Method = "HEAD";
// HttpWebRequest.GetResponse(): From MSDN: The actual instance returned
// is an HttpWebResponse, and can be typecast to that class to access 
// HTTP-specific properties. 
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
long len = resp.ContentLength;

Just realized it's exactly what's in How to get the file size from http headers (maybe this question should be marked as duplicate?).

Community
  • 1
  • 1
user276648
  • 6,018
  • 6
  • 60
  • 86