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
BytesRecievedproperty 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.BytesReceivedandFileSizeare integers then you will always have0 * 100 = 0.Make something like this:
double dProgress = ((double)e.BytesReceived / FileSize)*100.0It is because
/does integer division when dividing two integers. But you don’t want that. So you convert one of the variables todouble.