I am using webclient to dowlnoad a file. I am calculating the progress percentage as below
I know the filesize (I read it from the database table) of the file going to be downloaded.
I am depending on the BytesRecieved property of WebClient to know the total bytes fetched during download.
The algorithm I am using is double dProgress = (e.BytesReceived / FileSize)*100); to calculate the progress percentage.
However i am not getting correct progress percentage to update the progress bar.
Is there any method to calculate progress percentage?
Look at the following line:
double dProgress = (e.BytesReceived / FileSize)*100)
If both e.BytesReceived and FileSize are integers then you will always have 0 * 100 = 0.
Make something like this:
double dProgress = ((double)e.BytesReceived / FileSize)*100.0
It is because / does integer division when dividing two integers. But you don't want that. So you convert one of the variables to double.
BytesReceived and FileSize most probably are integers so you need to calculate progress this way:
double dProgress = 100.0 * e.BytesReceived / FileSize;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With