title: c# WebClient DownloadProgresschanged TotalBytesToReceive = -1
body: <p>I've had a look around the web and can't see anyone else with this problem. I'm using a web client using DownloadFileAsync and when the event handler (DownloadProgressChanged) is called the TotalBytesToReceive (from the DownloadProgressChangedArgs) is equalling -1 for some reason, thus stopping me from being able to use a progress bar. I'm detecting &lt;0 and if so just guessing 100meg for now, to get round it.</p>
  
  <p>The BytesRecieved is working however, and the file is actually being downloaded, and the AsynCompletedEventHadnler seems to be getting called so it knows its finished (so must know the TotalBytesToReceive somehow?).
  I'm using a WebClient with credentials and proxy credentials to download from a password protected external site going through an internal network (so needed both) - not sure if that would make any difference.</p>
  
  <p>I was previously using WebClient.DownloadData getting the byte data and saving it separately and putting it in a background worker,and it worked fine (if quite slow) but there was no way I could show progress this way. Also the DownloadFileAsync seems to do all of this for me so saved a lot of code.</p>
  
  <pre><code>    private void DLFile_AsyncWithStatus(string DLlocation, string un, string pw, string destLoc)
      {
          WebClient wc = new WebClient();
          wc.Credentials = new NetworkCredential(un, pw); // website login
          wc.Proxy.Credentials = new NetworkCredential(ProxyUsername, ProxyPassword, ProxyDomain); //proxy login
  
          Uri uri = new Uri(DLlocation);
  
          // Specify that the DownloadFileCallback method gets called when the download completes.
          wc.DownloadFileCompleted += new AsyncCompletedEventHandler(DLFile_AsynWithStatus_Completed);
          // Specify a progress notification handler.
          wc.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
          try
          {
              wc.DownloadFileAsync(uri, destLoc);
  
          }
          catch (WebException e)
          {
              MessageBox.Show(e.Message);
          }
  
      }
      private void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
      {
  
          double bytesIn = double.Parse(e.BytesReceived.ToString());
          double totalBytes = double.Parse(e.TotalBytesToReceive.ToString());
          if (totalBytes &lt; 0) { 
              totalBytes = 100.0 * 1000000.0; //guess 100 meg since it is not detecting total bytes
          } 
          double percentage = bytesIn / totalBytes * 100;
  
  
          lblTmpStatus.Text = "Downloaded " + e.BytesReceived + " of " + e.TotalBytesToReceive;
  
          progressBar1.Value = int.Parse(Math.Truncate(percentage).ToString());
      }
      void DLFile_AsynWithStatus_Completed(object sender, AsyncCompletedEventArgs e)
      {
          if (e.Error != null)
          {
              Msg(e.Error.Message);
          }
          else
          {
              progressBar1.Value = 100;//temp.. finish it off incase was less than 100 meg.
          }
      }
  </code></pre>
  
  <p></p>